目录
给定两个列表,找出相同元素和不同元素
numpy直接判断相等:
Python:给定两个列表,找出相同元素和不同元素
给定两个列表,找出相同元素和不同元素
list1 = [1, 2, 4]
list2 = [3, 4, 5]
set1 = set(list01) # 将列表转换成集合
set2 = set(list02)
print(set1 & set2) # 相同元素
print(set1 ^ set2) # 不同元素
输出结果为:
{4}
{1, 2, 3, 5}
#接口返回值
list1 = ['张三', '李四', '王五', '老二']
#数据库返回值
list2 = ['张三', '李四', '老二', '王七']
a = [x for x in list1 if x in list2] #两个列表表都存在
b = [y for y in (list1 + list2) if y not in a] #两个列表中的不同元素
print('a的值为:',a)
print('b的值为:',b)
c = [x for x in list1 if x not in list2] #在list1列表中而不在list2列表中
d = [y for y in list2 if y not in list1] #在list2列表中而不在list1列表中
print('c的值为:',c)
print('d的值为:',d)



