Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

python 中列表怎么分区间统计

list= [
    ...,
    2648, 2648, 2648, 63370, 63370, 425, 425, 120,
    120, 217, 217, 189, 189, 128, 128, 115, 115, 197,
    19752, 152, 152, 275, 275, 1716, 1716, 131, 131,
    98, 98, 138, 138, 277, 277, 849, 302, 152, 1571,
    68, 68, 102, 102, 92, 92, 146, 146, 155, 155,
    9181, 9181, 474, 449, 98, 98, 59, 59, 295, 101,
    ...
]

for i in list:
    if int(i/50)+1 not in dic:
        dic(int(i /50)+1)=list.count(i)
    else:
        dic(int(i /50)+1)+=list.count(i)

我这么写总是报错,我想以 50bp 为一个区间进行统计,即统计长度在 0-50 的频数,50-100 的频数...

我这么写对么,应该怎么写呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
# code for python3
from itertools import groupby

lst= [
    2648, 2648, 2648, 63370, 63370, 425, 425, 120,
    120, 217, 217, 189, 189, 128, 128, 115, 115, 197,
    19752, 152, 152, 275, 275, 1716, 1716, 131, 131,
    98, 98, 138, 138, 277, 277, 849, 302, 152, 1571,
    68, 68, 102, 102, 92, 92, 146, 146, 155, 155,
    9181, 9181, 474, 449, 98, 98, 59, 59, 295, 101, 5
]

for k, g in groupby(sorted(lst), key=lambda x: x//50):
    print('{}-{}: {}'.format(k*50, (k+1)*50-1, len(list(g))))

結果:

0-49: 1
50-99: 10
100-149: 15
150-199: 8
200-249: 2
250-299: 5
300-349: 1
400-449: 3
450-499: 1
800-849: 1
1550-1599: 1
1700-1749: 2
2600-2649: 3
9150-9199: 2
19750-19799: 1
63350-63399: 2

  1. 不要使用內建函數的名字: list 作為變數名

  2. 你想要的區間重疊了, 比如說元素 50 就不知道要分在 0-50 還是 50-100 了

  3. 存取字典應該用 [] 而不是 ()


我回答過的問題: Python-QA


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...