我的程序排序不正确

马修·伦佩尔(Mathew Rempel)

我从文件中制作了一个直方图,但我可以正常工作,但是无法正确地对其进行排序。含义100 90 50等

这是我的代码:

from collections import Counter
data=[]
with open("data.txt", 'r') as f:
    for line in f:
    line = line.strip()
    data.append(str(line))

counts = Counter(data)

for key, size in sorted(counts.items()):
    print('{}: {}'.format(key, int(size) * '*'))

这是输出:

100: ******
25: **
50: ***
60: *
65: *
70: **
75: *
80: ****
85: ****
90: ***

有什么建议么??

编辑:

我的意思是,它们按数字顺序排列。所以安装了100,25,50,....我要它100,90,85,.....

阿克斯

njzk2是绝对正确的,谢谢!您可以这样做的一种方式是这样的:

...
line = line.strip()
# casting to 'int' type just before populating in data table..
data.append(int(line)) 
...

那么你就可以

...
# applying reversed to reorder from ascending order to descending order.
for key, size in sorted(counts.items(), reverse=True):
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章