小编use*_*143的帖子

如何在python中将列表转换为字典

我有以下列表:

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)

我该怎么做?

python dictionary python-3.x

-4
推荐指数
1
解决办法
183
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1