在单独的行上显示“计数器”结果(Python)

在单独的行上显示“计数器”结果(Python),python,Python,我正在尝试获取它,以便此代码: from collections import Counter a = input('Votes: ') a = a.split(',') count = Counter(a) print (count) 当我输入如下内容时: One,One,Two,Two,Three,Four,Five,Five 打印此文件: One: 2 Two: 2 Three: 1 Four: 1 Five:2 与此相反: Counter({'One': 2, 'Two': 2,

我正在尝试获取它,以便此代码:

from collections import Counter
a = input('Votes: ')
a = a.split(',')
count = Counter(a)
print (count)
当我输入如下内容时:

One,One,Two,Two,Three,Four,Five,Five
打印此文件:

One: 2
Two: 2
Three: 1
Four: 1
Five:2
与此相反:

Counter({'One': 2, 'Two': 2, 'Five': 2, 'Three': 1, 'Four': 1})
在输出上循环:

最常用的方法按从最常用到最不常用的顺序为您提供项目:

如果您需要按照它们的“先到”顺序对它们进行排序,只因为首先提到了一个,那么就使用str.index按照它们的字符串索引对它们进行排序

如果需要按数字顺序排列,请使用字典将单词翻译成数字:

numbers = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} # expand as needed
for key, c in sorted(count.items(), key=lambda i: numbers[i[0].lower()]):
    print("{}: {}".format(key, c))
演示:

一艘班轮,已分拣:

a = ['One','One','Two','Two','Three','Four','Five','Five']
counts = Counter(a)
print('\n'.join('{}: {}'.format(*x) for x in sorted(counts.items(), key=lambda x: a.index(x[0]))))

设置一个集合以删除重复项,并使用带有lambda的sorted根据a列表中与输入顺序匹配的对应值的索引进行排序

是否需要对其进行排序?
numbers = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} # expand as needed
for key, c in sorted(count.items(), key=lambda i: numbers[i[0].lower()]):
    print("{}: {}".format(key, c))
>>> from collections import Counter
>>> a = 'One,One,Two,Two,Three,Four,Five,Five'.split(',')
>>> count = Counter(a)
>>> for key, c in count.most_common():
...     print("{}: {}".format(key, c))
... 
Five: 2
Two: 2
One: 2
Three: 1
Four: 1
>>> for key, c in sorted(count.items(), key=lambda i: a.index(i[0])):
...     print("{}: {}".format(key, c))
... 
One: 2
Two: 2
Three: 1
Four: 1
Five: 2
>>> numbers = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for key, c in sorted(count.items(), key=lambda i: numbers[i[0].lower()]):
...     print("{}: {}".format(key, c))
... 
One: 2
Two: 2
Three: 1
Four: 1
Five: 2
a = ['One','One','Two','Two','Three','Four','Five','Five']
counts = Counter(a)
print('\n'.join('{}: {}'.format(*x) for x in sorted(counts.items(), key=lambda x: a.index(x[0]))))
from collections import Counter
a = input('Votes: ')
a = a.split(',')
count = Counter(a)

for key in sorted(set(a),key=lambda x: a.index(x)):
     print ("{}: {}".format(key,count[key]))

In [13]: for key in sorted(set(a),key=lambda x: a.index(x)):
   ....:          print ("{}: {}".format(key,count[key]))
   ....:     

One: 2
Two: 2
Three: 1
Four: 1
Five: 2