473,383 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
Jul 18 '05 #1
3 80947
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 && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Only the ephemeral is of lasting value.
\__/ Ionesco
Jul 18 '05 #2
>>>>> "Mickel" == Mickel Grönroos <mi****@csc.fi> 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.
Jul 18 '05 #3
xoanan
1
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
Aug 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd MacCulloch | last post by:
Suppose I have a master list and I need to create two derived lists, something like: s0 = s1 = s2 = But suppose generating s0 is expensive and/or s0 is big. In otherwords I'd like to go...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> ...
7
by: Will | last post by:
I'm trying to build dynamic sql from a string passed by a calling application. Let's assume for this discussion that the user can pass a string of letters with these logical operators ("and",...
2
by: James Stroud | last post by:
Hello All, I find myself in this situation from time to time: I want to compare two lists of arbitrary objects and (1) find those unique to the first list, (2) find those unique to the second...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
3
by: GKRAFT | last post by:
I'm trying to create a function which would look like this when running: >> union(,) >> union(,) I have written the following code: def union(l1, l2): finalList = i = 0
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.