我只是想知道如何转换一个字符串,例如“你好,你好”,并将其转换为字典,然后使用这本字典,我想计算字典中每个单词的数量,并按字母顺序返回命令。所以在这种情况下它会返回:
[('hello', 1), ('hi', 1), ('there', 2)]
任何帮助,将不胜感激
最佳答案
>>> from collections import Counter
>>> text = "hello there hi there"
>>> sorted(Counter(text.split()).items())
[('hello', 1), ('hi', 1), ('there', 2)]
关于python - 如何将字符串转换为字典,并计算每个单词的数量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16557900/