小编Ale*_*nko的帖子

Python字典理解:为key赋值,其中value是一个列表

例:

dictionary = {"key":[5, "string1"], "key2":[2, "string2"], "key3":[3, "string1"]}
Run Code Online (Sandbox Code Playgroud)

应用此词典理解后:

another_dictionary = {key:value for (value,key) in dictionary.values()}
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

another_dictionary = {"string1": 5, "string2": 2}
Run Code Online (Sandbox Code Playgroud)

换句话说,它不会在作为列表项的相同键下汇总整数值.

================================================== ===============

期望的结果:

another_dictionary = {"string1": 8, "string2": 2}
Run Code Online (Sandbox Code Playgroud)

python dictionary

1
推荐指数
1
解决办法
59
查看次数

标签 统计

dictionary ×1

python ×1