繁体   English   中英

python在比较项目时迭代两个列表

[英]python iterate over the two lists while comparing items

我有两个列表,例如x = [1,2,3,4,4,5,6,7,7] y = [3,4,5,6,7,8,9,10] ,我想迭代比较项目时在两个列表上。 对于那些匹配,我想调用一些函数并从列表中删除它们,在这个例子中我应该最终得到x = [1,2]和y = [8,9,10]。 由于我的数据类型和比较运算符,集合不适用于此问题。

for i in x:
  for j in y:
    if i ==j:
       callsomefunction(i,j)
       remove i, j from x and y respectively

编辑:在发现提问的人根本不知道__hash__我在评论中提供了这些信息:

要使用集合,请实现__hash__ 因此,如果obj1 == obj2obj1.a == obj2.a and ob1.b == obj2.b__hash__应该return hash((self.a, self.b)) ,你的集合将按预期工作。

这解决了他们的问题,他们转而使用套装。

这个答案的其余部分现在已经过时了,但它仍然是正确的(但效率非常低)所以我会把它留在这里。


这段代码可以满足您的需求。 最后, newxnewyxy的非重叠项目。

x = [1,2,3,4,4,5,6,7,7]
y = [3,4,5,6,7,8,9,10]
# you can leave out bad and just compare against
# x at the end if memory is more important than speed
newx, bad, newy = [], [], []
for i in x:
    if i in y:
        callsomefunction(i)
        bad.append(i)
    else:
        newx.append(i)

for i in y:
    if i not in bad:
        newy.append(i)

print newx
print newy

但是,我知道甚至没有看到你的代码,这是错误的方法来做到这一点。 你当然可以用套装做,但如果你不想,那取决于你。

好的,丢弃我的帖子,我没有看到你提到的那些设置不起作用。

然而,如果你可以做一点工作,你可能想要使用类,以便运算符按照预期的那样工作

我认为最“蟒蛇式”的做法是使用套装。 然后你可以这样做:

x = set([1,2,3,4,4,5,6,7,7])
y = set([3,4,5,6,7,8,9,10])
for item in x.intersection(y): #y.intersection(x) is fine too.
    my_function(item) #You could use my_function(item, item) if that's what your function requires
    x.remove(item)
    y.remove(item)

我认为,当涉及到性能时,这些集合也比这类工作的列表更有效(尽管这可能不是您的首要任务)。

在旁注中,您还可以使用:

x,y = x.difference(y), y.difference(x) 

这有效地从x和y中移除x和y中的项目。

尝试这个:

for i in x:
    if i in y:
        callsomefunction(i)
        x.remove(i)
        y.remove(i)

编辑:更新的答案

这个怎么样:

import itertools
x = [1,2,3,4,4,5,6,7,7] 
y = [3,4,5,6,7,8,9,10]
output = map(somefunction, itertools.product(x,y))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM