lsq546397641的个人博客分享 http://blog.sciencenet.cn/u/lsq546397641

博文

python中的浮点数range方法

已有 4006 次阅读 2022-3-17 12:06 |系统分类:科研笔记

例子:

#python中的range函数支持步进,如下:

>>> print range(2,15,3)

[2, 5, 8, 11, 14]

#但是浮点数不支持range函数,自己定义一个类似的

from decimal import *

def floatrange(start,stop,steps):

    '''

    start:计数从 start 开始

    stop:计数到 stop 结束

    step:步长

    '''

    resultList = []

    while Decimal(str(start)) <= Decimal(str(stop)):

        resultList.append(float(Decimal(str(start))))

        start = Decimal(str(start))+Decimal(str(steps))

    return resultList


alleleFreqeuncy = floatrange(0.5,0.95,0.05)


返回:

print(alleleFreqeuncy)

[0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]




https://m.sciencenet.cn/blog-994715-1329827.html

上一篇:使用python语言,统计列表的四分位数
下一篇:vsearch软件cluster(聚类)结果解释

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 14:18

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部