Skip to main content
 首页 » 编程设计

python之如何确定一个元素是否在列表中

2025年05月04日9sharpest

thelist = [{'color':'green', 'time':4}, {'color':'red','time':2},{'color':'blue','time':5}] 

怎么说:

If "red" is in thelist and time does not equal 2 for that element (that's we just got from the list): 

请您参考如下方法:

使用 any()判断是否有满足条件的元素:

>>> any(item['color'] == 'red' and item['time'] != 2  for item in thelist) 
False