Goo*_*ose 4 python dictionary tuples list
我有一个清单
L = Counter(mywords)
Run Code Online (Sandbox Code Playgroud)
在哪里
mywords = ['Well', 'Jim', 'opportunity', 'I', 'Governor', 'University', 'Denver', 'hospitality', 'There', 'lot', 'points', 'I', 'make', 'tonight', 'important', '20', 'years', 'ago', 'I', 'luckiest', 'man', 'earth', 'Michelle', 'agreed', 'marry', '(Laughter)', 'And', 'I', 'Sweetie', 'happy']
Run Code Online (Sandbox Code Playgroud)
它比那要长得多,但这是一个片段。
现在我接下来要做的是:
print ("\n".join(c.most_common(10)))
Run Code Online (Sandbox Code Playgroud)
因为我希望它显示该列表中 10 个最常用的单词及其计数,但我希望它为列表中的每个项目打印出新行,而不是我收到此错误:
TypeError: sequence item 0: expected str instance, tuple found
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,使用 Python 3。
print ("\n".join(map(str, c.most_common(10))))
Run Code Online (Sandbox Code Playgroud)
如果你想更多地控制格式,你可以使用这样的格式字符串
print ("\n".join("{}: {}".format(k,v) for k,v in c.most_common(10)))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15072 次 |
最近记录: |