[英]Python - Filter list of dictionaries based on multiple keys
Say I have the dictionary: 说我有字典:
myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'},
{'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
{'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'}
{'first': 'Robert', 'last': 'Antonio'}]
And I have a list called keys: 我有一个名为键的列表:
keys = ["middle", "last"]
I want to filter myDict based on each value in keys, which would result in 我想根据键中的每个值来过滤myDict,这会导致
filteredDict = [{'middle': 'Smith', 'last': 'Joule'},
{'middle': 'Johnson', 'last': 'Watt'},
{'middle': 'Edward', 'last': 'Doppler'},
{'last': 'Antonio'}]
As seen in the list dictionary in myDict list, the dictionary DOESNT have to have both keys in order to be placed into filteredDict. 如myDict列表中的列表字典中所示,字典DOESNT必须具有两个键才能被放入filteredDict中。 Is there an easy way to do this with dictionary comprehension in Python? 在Python中使用字典理解是否有一种简单的方法可以做到这一点?
With list comprehensions: 使用列表推导:
myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'},
{'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
{'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
{'first': 'Robert', 'last': 'Antonio'}]
keys = {"middle", "last"}
l = [{k:v for k, v in i.items() if k in keys} for i in myDict]
But you can also use map for this: 但您也可以使用map来实现:
myDict = [{'first': 'James', 'middle': 'Smith', 'last': 'Joule'},
{'first': 'James', 'middle': 'Johnson', 'last': 'Watt'},
{'first': 'Christian', 'middle': 'Edward', 'last': 'Doppler'},
{'first': 'Robert', 'last': 'Antonio'}]
keys = {"middle", "last"}
l = list(map(lambda x: {k:v for k, v in x.items() if k in keys}, myDict))
print(l)
output: 输出:
[{'last': 'Joule', 'middle': 'Smith'}, {'last': 'Watt', 'middle': 'Johnson'}, {'last': 'Doppler', 'middle': 'Edward'}, {'last': 'Antonio'}]
Use neverwalkaloner 's answer if you are just doing this once. 如果你只是这样做一次,请使用neverwalkaloner的答案。 But, if you find yourself manipulating lists of dictionaries often, I've written a free library called PLOD that streamlines much of this. 但是,如果你发现自己经常操纵词典列表,我写了一个名为PLOD的免费库,它简化了大部分内容。
>>> from PLOD import PLOD
>>> l = PLOD(myDict).dropKey("middle").returnList()
>>> l
[{'last': 'Joule', 'first': 'James'}, {'last': 'Watt', 'first': 'James'}, {'last': 'Doppler', 'first': 'Christian'}, {'last': 'Antonio', 'first': 'Robert'}]
>>> print(PLOD(l).returnString())
[
{first: 'James' , last: 'Joule' },
{first: 'James' , last: 'Watt' },
{first: 'Christian', last: 'Doppler'},
{first: 'Robert' , last: 'Antonio'}
]
>>>
The library is on PyPi: https://pypi.python.org/pypi/PLOD 该库位于PyPi上: https ://pypi.python.org/pypi/PLOD
To more universally do what you want, I'd need to add a new class method. 为了更普遍地做你想要的,我需要添加一个新的类方法。 Perhaps .filterKeys
. 也许.filterKeys
。 Perhaps I'll do that in version 1.8. 也许我会在1.8版本中这样做。 It would then go: 然后会:
>>> l = PLOD(myDict).filterKeys(['first', 'last']).returnList()
Hmmm... 嗯...
BTW, the lib supports Python 2.7.x right now. 顺便说一下,lib现在支持Python 2.7.x. Still working on the 3.5.x release. 仍在使用3.5.x版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.