use*_*143 -4 python dictionary python-3.x
我有以下列表:
pet = ['cat','dog','fish','cat','fish','fish']
Run Code Online (Sandbox Code Playgroud)
我需要将它转换为这样的字典:
number_pets= {'cat':2, 'dog':1, 'fish':3}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
Ash*_*ary 10
用途collections.Counter:
>>> from collections import Counter
>>> pet = ['cat','dog','fish','cat','fish','fish']
>>> Counter(pet)
Counter({'fish': 3, 'cat': 2, 'dog': 1})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
183 次 |
| 最近记录: |