Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

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


You want to use Sets, introduces in Python 2.3:

http://www.python.org/dev/doc/devel/...t-example.html says:
>>> from sets import Set
>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
>>> management = Set(['Jane', 'Jack', 'Susan', 'Zack'])
>>> employees = engineers | programmers | management # union
>>> engineering_management = engineers & programmers # intersection
>>> fulltime_management = management - engineers - programmers # difference
>>> engineers.add('Marvin') # add element
>>> print engineers

Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
>>> employees.issuperset(engineers) # superset test

False
>>> employees.update(engineers) # update from another set
>>> employees.issuperset(engineers)

True
>>> for group in [engineers, programmers, management, employees]:

.... group.discard('Susan') # unconditionally remove element
.... print group
....
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
Set(['Janice', 'Jack', 'Sam'])
Set(['Jane', 'Zack', 'Jack'])
Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])

See also:

http://www.python.org/dev/doc/devel/...dule-sets.html
http://www.python.org/peps/pep-0218.html
http://groups.google.nl/groups?hl=nl...t%40python.org

yours,
Gerrit.

--
131. If a man bring a charge against one's wife, but she is not
surprised with another man, she must take an oath and then may return to
her house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/

 
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
Standard ways to get union, intersection, difference of lists? =?ISO-8859-1?Q?Mickel_Gr=F6nroos?= Python 2 06-26-2003 09:28 AM



Advertisments