小编jad*_*dki的帖子

从词典列表中创建词典子集

我有一个字典列表,如下所示:

d = [{'first':'jason','color':'green','age':22},
     {'first':'josh','color':'red','age':22},
     {'first':'chris','color':'blue','age':21}
    ]
Run Code Online (Sandbox Code Playgroud)

我想创建一个字典,它是以前字典的一个子集.

看起来像:

newD = {'jason':22, 'josh':22, 'chris':21}
Run Code Online (Sandbox Code Playgroud)

以下是诀窍:

first = [k['first'] for k in d]
age = [k['age'] for k in d]
newD = dict(zip(first, age))
Run Code Online (Sandbox Code Playgroud)

但有没有更多Pythonic /更清洁的方法来做到这一点?

python dictionary

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

标签 统计

dictionary ×1

python ×1