thelist = [{'color':'green', 'time':4}, {'color':'red','time':2},{'color':'blue','time':5}]
Run Code Online (Sandbox Code Playgroud)
我怎么说:
If "red" is in thelist and time does not equal 2 for that element (that's we just got from the list):
Run Code Online (Sandbox Code Playgroud)
sth*_*sth 13
使用any()以找出是否有满足条件的元素:
>>> any(item['color'] == 'red' and item['time'] != 2 for item in thelist)
False
Run Code Online (Sandbox Code Playgroud)