两个列表中的值

时间:2018-10-16 12:17:25

标签: python list if-statement

所以我有两个清单。例如:

C = [(*[j[0] for j in A if j[1] == i], i) for i in B] 
# [(1, 4), (5, 2), (11, 3), (1, 4), (10, 8), (59, 14), (1, 4), (5, 2)]

我只希望将在list2中唯一显示的值追加到新列表中。例如:

list1 = [a1, b2, c3, f6]
list2 = [a1, b2, d4, e5]

2 个答案:

答案 0 :(得分:2)

您可以使用list comprehension

list3 = [item for item in list2 if item not in list1]

输出

list3 = ['d4', 'e5']

答案 1 :(得分:0)

您可以先将attrs="{'invisible': [('company_ids', '!=', [(6, False, [3])])]}" attrs="{'invisible': [('company_ids', '!=', 3)]}" attrs="{'invisible': [('company_ids', 'not in', [3])]}" attrs="{'invisible': [('company_ids', 'child_of', [(6, False, [4])])]}" attrs="{'invisible': [('company_ids', 'child_of', [4])]}" attrs="{'invisible': [('company_ids', 'not in', 3])]}" attrs="{'invisible': [('company_ids', '!=', '3']}" attrs="{'invisible': [('company_ids', '!=', 'Company Name')]}" 转换为集合,以便在迭代list1时有效地测试list2中的项目是否在集合中:

list2

list1 = ['a1', 'b2', 'c3', 'f6'] list2 = ['a1', 'b2', 'd4', 'e5'] set1 = set(list1) list3 = [i for i in list2 if i not in set1] 将变为:

list3