如何检查另一个列表中的列表

时间:2019-06-14 12:29:27

标签: python-3.x

我在列表中有一个列表:

a = [
  [123123, 'juststring', '129.123.41.4'],
  [456456, 'usrnm', '129.123.41.4'],
  [78970, 'Something', '129.123.41.4']
]

我还有另一个列表:

b = [123123, 354634, 54234, 6734]

如果ba中包含数字,则必须输入'YES'或'NO'

输出:

a = [[123123, 'juststring', '129.123.41.4', 'YES'], [456456, 'usrnm', '129.123.41.4', 'NO'], [78970, 'Something', '129.123.41.4', 'NO']]

这是我的代码:

for i in range(len(tbl_list)):
    for l in tbl_list:
        for p in pid:
            if int(l[0]) == int(p):
                tbl_list[i].append('YES')
                break
            else:
                tbl_list[i].append('NO')
                break
def draw_table():
    global tbl_list
    global pid
    for i in range(len(tbl_list)):
        for l in tbl_list:
            for p in pid:
                if int(l[0]) == int(p):
                    tbl_list[i].append('YES')
                    break
                else:
                    tbl_list[i].append('NO')
                    break
        tbl.add_row(l)
    print(tbl_list)
    print(tbl.draw())
    tbl.reset()
    tbl.header(Heading)

1 个答案:

答案 0 :(得分:2)

您可以这样做:

a = [[123123, 'juststring', '129.123.41.4'], [456456, 'usrnm', '129.123.41.4'], [78970, 'Something', '129.123.41.4']]

b = [123123, 354634, 54234, 6734]

for list_a in a:
    if any(pid == list_a[0] for pid in b):
        list_a.append('YES')
    else:
        list_a.append('NO')

print(a)