我有一个类似这样的dict
:
"votes": {
"user1": "yes",
"user2": "no",
"user3": "yes",
"user4": "no",
"user5": "maybe",
"user6": "yes"
}
我想做的是,统计相同的值,以便我知道yes
出现了3次,no
出现了2次,maybe
出现了1次。
目前我的做法是这样的:
votes = OrderedDict()
for key, value in vote_dict["votes"].items():
if value in votes:
votes[value] += 1
else:
votes[value] = 1
这样做是可行的,但肯定有更好的方法。有什么更符合Python风格的方法吗?
dict
的子类,因此collections.Counter
也是按插入顺序排序的。 - jpp