使用:all()
_dict.items()
In [66]: lis=[
....: {"START":"Denver", "END":"Chicago", "Num":0},
....: {"START":"Dallas", "END":"Houston", "Num":3},
....: {"START":"Virginia", "END":"Boston", "Num":1},
....: {"START":"Washington", "END":"Maine", "Num":7}
....: ]
In [67]: for x in lis:
....: if all(y in x.items() for y in search.items()):
....: x['Num']="foobar" #change Num here
....:
In [68]: lis
Out[68]:
[{'END': 'Chicago', 'Num': 0, 'START': 'Denver'},
{'END': 'Houston', 'Num': 3, 'START': 'Dallas'},
{'END': 'Boston', 'Num': 'foobar', 'START': 'Virginia'},
{'END': 'Maine', 'Num': 7, 'START': 'Washington'}]
使用list comprehension
:
In [58]: [x for x in lis if all(y in x.items() for y in search.items())]
Out[58]: [{'END': 'Boston', 'Num': 1, 'START': 'Virginia'}]