Jam*_*son 4 python string dictionary python-3.x
我只是想知道我将如何转换字符串,如"你好那里好",然后把它变成字典,然后使用这个字典,我想计算字典中每个单词的数量,并以字母顺序返回订购.所以在这种情况下它会返回:
[('hello', 1), ('hi', 1), ('there', 2)]
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激
jam*_*lak 14
>>> from collections import Counter
>>> text = "hello there hi there"
>>> sorted(Counter(text.split()).items())
[('hello', 1), ('hi', 1), ('there', 2)]
Run Code Online (Sandbox Code Playgroud)
class collections.Counter([iterable-or-mapping])A
Counter是dict用于计算可哈希对象的子类.它是一个无序集合,其中元素存储为字典键,其计数存储为字典值.计数允许为任何整数值,包括零或负计数.该Counter班是类似于其他语言包或者多集.
| 归档时间: |
|
| 查看次数: |
5072 次 |
| 最近记录: |