我有一个包含这样字典的列表:
[{"abc":"da123-tap","efg":"xyzf","acd":"123-brf"}, {"abc":"ab234-tap","efg":"yuvi","acd":"345-brf"}]
我希望列表中的abc
的所有值(list1
)和efg
的所有值都列在另一个列表中(list2
)。
更新问题:
我读了一个文件来获取这个词典列表:
[{"Sample":"da123-tap","Name":"john","dof":"Date(304239600000)","Account":"Clinic1"},
{"Sample":"da456-tap-tap","Name":"smith","dof":"Date(304239600000)","Account":"Clinic2"},
{"Sample":"da678-tap-tap","Name":"jane","dof":"Date(304239600000)","Account":"Clinic1"},
{"Sample":"da987-tap-tap","Name":"doe","dof":"Date(304239600000)","Account":"Clinic2"}]
查找
Sample
list1
的值
Account
list2
的值
Sample
Clinic1
的值
Sample
Clinic2
的值
醇>
答案 0 :(得分:0)
仅出于教育原因,将不同键的值导入多个列表的优雅方法可能是使用zip()
并解压缩:
>>> l = [
... {"abc":"da123-tap","efg":"xyzf","acd":"123-brf"},
... {"abc":"ab234-tap","efg":"yuvi","acd":"345-brf"}
... ]
>>>
>>> a, b = zip(*[(item["abc"], item["efg"]) for item in l])
>>> print(a)
('da123-tap', 'ab234-tap')
>>> print(b)
('xyzf', 'yuvi')