加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python – 在理解中比较两个列表

发布时间:2020-12-20 13:02:12 所属栏目:Python 来源:网络整理
导读:我试图理解理解是如何运作的. 我想循环遍历两个列表,并比较每个列表以找出差异. 如果一个/多个单词不同,我想打印这个单词. 我喜欢这一切的代码,这就是为什么我对理解感兴趣. 解决方法 像kriegar建议使用套装可能是最简单的解决方案.如果你绝对需要使用列表理
我试图理解理解是如何运作的.

我想循环遍历两个列表,并比较每个列表以找出差异.
如果一个/多个单词不同,我想打印这个单词.

我喜欢这一切的代码,这就是为什么我对理解感兴趣.

解决方法

像kriegar建议使用套装可能是最简单的解决方案.如果你绝对需要使用列表理解,我会使用这样的东西:

list_1 = [1,2,3,4,5,6]
list_2 = [1,6]

# Print all items from list_1 that are not in list_2 ()
print(*[item for item in list_1 if item not in list_2],sep='n')

# Print all items from list_1 that differ from the item at the same index in list_2
print(*[x for x,y in zip(list_1,list_2) if x != y],sep='n')

# Print all items from list_2 that differ from the item at the same index in list_1
print(*[y for x,sep='n')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读