gpt4 book ai didi

python - 比较两个列表,保留重复项但删除 Python 中索引位置中的相同项

转载 作者:行者123 更新时间:2023-12-03 20:25:45 28 4
gpt4 key购买 nike

我有两个列表:

List1 = ['One', 'Two', 'Three', 'Five', 'Seven', 'Ten', ' Two', 'One'] 
List2 = ['Nine', 'Two', 'Seven', 'Five' , 'Five', 'Three', 'One', 'One']


列表大小相同。

我想要的是,通过索引将 list1 与 list2 匹配,如果相应的索引项相同,则删除 list2 中的匹配项。否则它不应该删除项目。 Newlist2 中的非索引位置可能存在重复项。

这是我的期望:
List1:#Same as the previous

NewList2 = ['Nine', 'Seven', 'Five', 'Three', 'One']

最佳答案

NewList2 = [ y for (x, y) in zip(List1, List2) if x != y ] 

关于python - 比较两个列表,保留重复项但删除 Python 中索引位置中的相同项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61592009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com