简体   繁体   English

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

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

i have two lists eg x = [1,2,3,4,4,5,6,7,7] y = [3,4,5,6,7,8,9,10] , i want to 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] ,我想迭代比较项目时在两个列表上。 For those that match, i would like to call some function and remove them from the lists, in this example i should end up with x= [1,2] and y = [8,9,10]. 对于那些匹配,我想调用一些函数并从列表中删除它们,在这个例子中我应该最终得到x = [1,2]和y = [8,9,10]。 Sets will not work for this problem because of my type of data and the comparison operator. 由于我的数据类型和比较运算符,集合不适用于此问题。

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

Edit: After discovering the person asking the question simply didn't know about __hash__ I provided this information in a comment: 编辑:在发现提问的人根本不知道__hash__我在评论中提供了这些信息:

To use sets, implement __hash__ . 要使用集合,请实现__hash__ So if obj1 == obj2 when obj1.a == obj2.a and ob1.b == obj2.b , __hash__ should be return hash((self.a, self.b)) and your sets will work as expected. 因此,如果obj1 == obj2obj1.a == obj2.a and ob1.b == obj2.b__hash__应该return hash((self.a, self.b)) ,你的集合将按预期工作。

That solved their problem, and they switched to using sets. 这解决了他们的问题,他们转而使用套装。

The rest of this answer is now obsolete, but it's still correct (but horribly inefficient) so I'll leave it here. 这个答案的其余部分现在已经过时了,但它仍然是正确的(但效率非常低)所以我会把它留在这里。


This code does what you want. 这段代码可以满足您的需求。 At the end, newx and newy are the non-overlapping items of x and y specifically. 最后, 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

However, I know without even seeing your code that this is the wrong way to do this. 但是,我知道甚至没有看到你的代码,这是错误的方法来做到这一点。 You can certainly do it with sets, but if you don't want to, that's up to you. 你当然可以用套装做,但如果你不想,那取决于你。

Ok, discard my post, I hadn't seen the point where you mentionned that sets wouldn't work. 好的,丢弃我的帖子,我没有看到你提到的那些设置不起作用。

Nevertheless, if you're OK with a little work, you might want to use classes so that operators do work as they are expected to. 然而,如果你可以做一点工作,你可能想要使用类,以便运算符按照预期的那样工作

I think the most "pythonistic" way of doing this is to use sets. 我认为最“蟒蛇式”的做法是使用套装。 You could then do : 然后你可以这样做:

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)

I think that sets are also more efficient than lists for this kind of work when it comes down to performance (though this might not be your top priority). 我认为,当涉及到性能时,这些集合也比这类工作的列表更有效(尽管这可能不是您的首要任务)。

On a sidenote, you could also use: 在旁注中,您还可以使用:

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

This effectively removes items that are in x and y from x and y. 这有效地从x和y中移除x和y中的项目。

Try this: 尝试这个:

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

EDIT: updated answer 编辑:更新的答案

how about this: 这个怎么样:

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