David Spira
David Spira

Reputation: 153

Intersecting lists in python won't work

I've been having trouble in performing an algorythm to intersect two lists in my code. I took this discussion (link below) as a reference and nothing seems to be working the way i expected. Find intersection of two lists?

I have this json file and am writing a code which will allow to perform search on its thousands items according to different (and simultaneous) criteria. At some point of the code, a list that contains all items of the json file that have already been found (according to the first criteria) must be intersected to the list generated from a second search (so that the resulting list will contain the items that satisfy BOTH criteria)

I used different algorythms to perform the intersection.

def intersect(a, b):
    for k in a:
        print "for k in a"
        if k not in b:
            print "if k not b"
            a.remove(k)
    return a

a = intersect(a, b)

i also tried both that are mentioned in the link above, not to mention others i made.

The result is that i don't get the intersected list as a result. sometimes it doesn't intersect at all, sometimes i don't know what goes wrong. With the algorythm above, the remove() function simply didn't remove anything.

Upvotes: 2

Views: 246

Answers (1)

Alex Alifimoff
Alex Alifimoff

Reputation: 1849

What about

def intersect(a, b): return [x for x in a if x in b]

if a and b are both lists, this should work fine.

As Tom rightly points out in the comments, this is a slow algorithm.

def intersect(a, b):
    sb = set(b)
    return [x for x in a if x in sb]

should be faster.

If you're interested in a rough comparison of these two algorithms, check out this blog post

Upvotes: 3

Related Questions