我有这个输出:
[(3, 'one'), (2, 'was'), (2, 'two'), (1, 'too'), (1, 'racehorse'), (1, 'a')]
我需要做的是让相同数字的元组在列表中按相反的字母顺序排列。这是我的代码:
`def top5_words(text):
split_text = text.split()
tally = {}
for word in split_text:
if word in tally:
tally[word] += 1
else:
tally[word] = 1
vals = []
for key, val in tally.items():
vals.append((val, key))
reverse_vals = sorted(vals, reverse = True)
return reverse_vals`
我输入的文字是:一个是赛马二个也是一个
定义列表:
对列表排序:
可以将^{} 与相反的参数一起使用:
给你:
相关问题 更多 >
编程相关推荐