300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Python查找相同元素 不同元素

Python查找相同元素 不同元素

时间:2023-11-15 18:12:38

相关推荐

Python查找相同元素 不同元素

目录

给定两个列表,找出相同元素和不同元素

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)

numpy直接判断相等:

(np.array(self.cls_1)==df_label.columns[1:]).all()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。