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 3 80950
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
>>>>> "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.
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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=
>>> ...
|
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",...
|
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...
|
by: Petronius |
last post by:
Hallo, does anyone have an idea how to implement difference lists in
Javascript?
Thanks allot in advance
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| | |