词频统计,字典成绩排序
# 统计词频 from random import randint from collections import Counter date = [randint(1, 10) for x in range(1, 21)] # date1 = {k:randint(1,50) for k in range(20)} # 随机生成20个元素的字典 print(date) c = Counter(date) print(c[5]) # 统计5出现的次数 print(c.most_common(5)) # 统计出频率最高的5个数 # 成绩统计 sore = {k: randint(60, 100) for k in "abcdefghij"} # 随机生成一个成绩字典 print(sore) # print(sore.keys(),sore.values(),sore.items()) print(sore.items()) # 将列表转化为元组 x = sorted(sore.items(), key=lambda x: x[1]) # 排序 x.reverse() # 反序 print(x)