collections是Python内建的一个集合模块,提供了许多有用的集合类。
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
查询大学信息中名称重复的学校
from collections import Counter
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient["gaokao"]
mycol = mydb["college"]
m_name =[]
for x in mycol.find({'code':{'$exists':'true'}},{'_id':0,'code':1,'name':1}):
#print(x)
m_name.append(x['name'])
#print(m_name)
b = dict(Counter(m_name))
print ([key for key,value in b.items()if value > 1]) #只展示重复元素
print ({key:value for key,value in b.items()if value > 1})