a,b,c = "a","b","c"
inp = [a,a,a,b,b,c,c,c,c]
dic = {}
for i in inp:
if i in dic:
dic[i]+=1
else:
dic[i] = 1
print(dic) #Dict with input values and count of them
print(dic.values()) #Count of values in the dict
from collections import OrderedDict
a,b,c = "a","b","c"
inp = [a,a,a,b,b,c,c,c,c]
dic = OrderedDict()
for i in inp:
if i in dic:
dic[i]+=1
else:
dic[i] = 1
print(dic)
print(dic.values())
用听写器做一个计数器。你知道吗
请记住,这会更改输入列表的顺序。 要保持订单完整,请使用Collections库中的orderedict方法。你知道吗
如果给定
['a', 'a', 'a', 'b', 'b', 'a']
时需要[3, 2, 1]
:相关问题 更多 >
编程相关推荐