我已经生成了150个质量值的列表。我需要从该列表中选取0.2-0.5、0.5-1.2等之间的值,然后找到列表中这些部分的中位数和范围,但是我尝试的每个函数都需要整数项和范围限制。有浮游物的等效项吗?我尝试了range()
和np.where
等。
答案 0 :(得分:0)
您可以尝试以下方法:
from random import random
from statistics import median
masses = [ random()*10 for _ in range(150) ]
intervals = [ (0.2,0.5),(0.5,1.2) ]
for (low,high) in intervals:
inRange = [ mass for mass in masses if mass >= low and mass < high ]
print("(",low,",",high,") count=",len(inRange),"median=",median(inRange), "range=", min(inRange),"-",max(inRange))