【发布时间】:2021-11-15 23:28:38
【问题描述】:
假设有一个列表列表。如何循环遍历列表的第二个元素并检查其他列表中的所有元素是否都存在?
list = [['foo', 2, 'bar'], ['foo', 5, 'bar'], ['foo', 9, 'bar'], ['foo', 12, 'bar']]
required = [ 2, 5, 9]
if all (item in list for item[1] in required):
log.debug('all items exist in nested list') //<- this should be printed
item[1] 在示例中是错误的,我不知道如何更改它。
【问题讨论】:
-
这里的
item是什么?迭代变量还是其他? -
@AmberBhanarkar 是的,它是一个迭代变量,目的是遍历列表的每个元素。
标签: python-3.x list loops