txt = open("命运.txt", "r", encoding="utf-8").read() #正式考试可以不用指定编码,用系统默认。
for ch in "↲":
txt = txt.replace(ch, "")
d = {}
for ch in txt:
d[ch] = d.get(ch, 0) + 1#将信息转换成字典
d_order=sorted(d.items(),key=lambda x:x[1],reverse=True)
for i in d_order[:11]:
print(str(i[0]),end="")
这是我的代码,
txt = open("命运.txt", "r", encoding="utf-8").read()
for ch in '\n':
txt = txt.replace(ch, "")
d = {}
for ch in txt:
d[ch] = d.get(ch, 0) + 1
ls = list(d.items())
ls.sort(key=lambda x: x[1], reverse=True) # 此行可以按照词频由高到低排序
for i in range(10):
print(str(ls[i])[2], end="")
这是参考答案
**
**
结果十分魔幻
请问为什么会这样?