Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Standard ways to get union, intersection, difference of lists?

Reply
Thread Tools

Standard ways to get union, intersection, difference of lists?

 
 
=?ISO-8859-1?Q?Mickel_Gr=F6nroos?=
Guest
Posts: n/a
 
      06-26-2003
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


 
Reply With Quote
 
 
 
 
Paul Rudin
Guest
Posts: n/a
 
      06-26-2003
>>>>> "Mickel" == Mickel Grönroos <> writes:

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


[snip]

> I realize I could quite easily implement this myself, but I was
> hoping for a built-in solution.


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.
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      06-26-2003
Mickel Grönroos wrote:

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


If you have Python 2.3 available, why not use a set?

>>> import sets
>>> s1 = sets.Set([1, 2, 3])
>>> s2 = sets.Set([3, 4])
>>> s1.intersection(s2)

Set([3])
>>> s1.difference(s2)

Set([1, 2])
>>> s2.difference(s1)

Set([4])

--
Erik Max Francis && && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Only the ephemeral is of lasting value.
\__/ Ionesco
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between two ways to set Text in a TextBox ? Moe Sisko ASP .Net 0 03-31-2008 05:59 AM
what's the difference betwwen these two ways to use std::ofstream thinktwice C++ 2 08-25-2006 05:44 AM
the difference among these ways of swapping two ints storyGerald@gmail.com C++ 9 11-21-2005 12:32 PM
Re: Standard ways to get union, intersection, difference of lists? =?ISO-8859-1?Q?Mickel_Gr=F6nroos?= Python 3 06-26-2003 05:27 PM
Re: Standard ways to get union, intersection, difference of lists? Gerrit Holl Python 0 06-26-2003 09:29 AM



Advertisments