简体   繁体   中英

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. 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]. 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:

To use sets, implement __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.

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.

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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