我有问题。我必须计算列表中元素的频率,并且必须按字母顺序打印这些元素。任务是:https://1drv.ms/i/s!AqbJai99OU_ejy-jF_wOYaQCFjHR
我这样说:
**>>>from collections import Counter
>>>text= "he he he he she it she it she it and and and "
>>>words=text.split()
>>>counter=Counter(words)
>>>top_three=counter.most_common(4)
>>>top_three.sort(key=lambda x: (-x[1], x[0]))
>>>print(top_three)**
但是当我进行测试运行时,我遇到了一个问题:https://1drv.ms/i/s!AqbJai99OU_ejzBadWfydEv6oGpa
我知道我必须使用count\u words函数,但我不知道怎么做。 如果你知道,请写信给我:D
这是一种更简单的方法来获得相似单词的计数并对它们进行排序。你知道吗
@D.Tarik下面的代码可以按照你的要求工作。如果你认为答案正确并且对你有帮助,请接受。你知道吗
输出:
相关问题 更多 >
编程相关推荐