Standard ways to get union, intersection, difference of lists?
Question posted by: Mickel Grönroos
(Guest)
on
July 18th, 2005 12:07 AM
Hi!
Are there any standard list methods for getting the intersection and
difference of two lists? (The union is easy ("list1.extend(list2)"),
unless you want it to contain unique values.)
Here is what I would like to have:
list1 = [1,2,3]
list2 = [3,4]
list3 = list1.intersection(list2)
(list3 is now [3])
list3 = list1.difference(list2)
(list3 is now [1,2])
list3 = list2.difference(list1)
(list3 is now [4])
I realize I could quite easily implement this myself, but I was hoping for
a built-in solution.
Cheers,
/Mickel
--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
July 18th, 2005 12:07 AM
# 2
|
Re: Standard ways to get union, intersection, difference of lists?
Mickel Grönroos wrote:
[color=blue]
> Are there any standard list methods for getting the intersection and
> difference of two lists? (The union is easy ("list1.extend(list2)"),
> unless you want it to contain unique values.)[/color]
If you have Python 2.3 available, why not use a set?
[color=blue][color=green][color=darkred]
>>> import sets
>>> s1 = sets.Set([1, 2, 3])
>>> s2 = sets.Set([3, 4])
>>> s1.intersection(s2)[/color][/color][/color]
Set([3])[color=blue][color=green][color=darkred]
>>> s1.difference(s2)[/color][/color][/color]
Set([1, 2])[color=blue][color=green][color=darkred]
>>> s2.difference(s1)[/color][/color][/color]
Set([4])
--
Erik Max Francis && Join Bytes! && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Only the ephemeral is of lasting value.
\__/ Ionesco
|
|
July 18th, 2005 12:07 AM
# 3
|
Re: Standard ways to get union, intersection, difference of lists?
>>>>> "Mickel" == Mickel Grönroos <mickel@csc.fi> writes:
[color=blue]
> Hi! Are there any standard list methods for getting the
> intersection and difference of two lists? (The union is easy
> ("list1.extend(list2)"), unless you want it to contain unique
> values.)[/color]
[snip]
[color=blue]
> I realize I could quite easily implement this myself, but I was
> hoping for a built-in solution.[/color]
You might want to take a look at the sets module in Python 2.3. It's
not exactly what you ask for since sets.Sets are not lists - but they
do support these operations and can easily be converted to or from
lists.
|
|
August 9th, 2005 08:49 PM
# 4
|
Re: Standard ways to get union, intersection, difference of lists?
I know lots of people hate using filter and lambda calls but this will give you a union between 2 lists, example:
[COLOR=Green]list1=range(5) # [0,1,2,3,4]
list2=range(3,7) # [3,4,5,6]
union=list1+filter(lambda x:x not in list1,list2)
# union = [0,1,2,3,4,5,6]
[/COLOR]
Intersection is just as easy
[COLOR=Green]
intersection=filter(lambda x:x in list1,list2)
# intersection=[3,4]
[/COLOR]
Difference is the last thing you wanted...
[COLOR=Green]
difference=filter(lambda x:x not in list2,list1)
# difference=[0,1,2]
[/COLOR]
And my set vocabulary is rusty but the distinct elements or those from both lists that are NOT in common (un-intersection?) would be:
[COLOR=Green]
distinct=filter(lamba x:x not in list2,list1)+filter(lambda x:x not in list1,list2)
# distinct=[0,1,2,5,6]
[/COLOR]
. -. -.. - .-. .- -. ... -- .. ... ... .. --- -.
Xoanan
Last edited by xoanan : August 9th, 2005 at 08:56 PM.
Reason: added one more example...
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|
|
|
Top Community Contributors
|