filtering random values (sieves?)

Jeff Shannon jeff at ccvcorp.com
Thu Feb 14 13:53:41 EST 2002


kevin parks wrote:

> But what i was hoping to do was generate two somewhat large lists
> algorhytmically and use one list to filter the other, which would be
> sort of  like getting the interesection, ....

You may not need this, but here's an easy way to get the intersection of
two lists... (presuming 2.x)

intersection = [x for x in list1 if x in list2]

If you need to work with 1.5.2 as well, then you could use filter/lambda.
I *think* that it would go like this:

intersection = filter(lambda x: x in list2, list1)

But I'm not certain enough about lambda's syntax to want to promise
anything about that.  ;)

Then once you have that, you can easily do the random.choice(intersection)
bit...

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list