使用all()
與dict.items()
:使用list comprehension
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'}]
:
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'}]
保證唯一性嗎?訂單可以改變嗎? – thegrinner
是的,我保證唯一性,訂購不能改變。 – alwbtc
我試圖改變「Num」的值 – alwbtc