February 27th, 2006, 03:50 AM
Union and Intersection of lists
Are there functions for obtaining the union and intersection of two lists or does it have to be coded?
Thanks for your help.
February 27th, 2006, 03:59 AM
I just found out that lists can be converted to sets and then using "and" their intersection can be obtained and using "or" their union, and finally "-" for obtaining elements found in one set but not the other.
My question now is whether a set is the same as a list in python without redundant elements?
As I have mentioned in my next posting also, it seems that Union and Intersection are not working properly, so I have added this editing.
Last edited by sam_kh918; February 27th, 2006 at 02:33 PM.
Reason: Union and Intersection not working properly!
February 27th, 2006, 04:06 AM
If by redundant you mean non-unique elements then yes.
Originally Posted by sam_kh918
February 27th, 2006, 12:01 PM
Could either of you post some code? I don't even know what a set is, but I'd be interested in seeing how you got that done!
February 27th, 2006, 12:37 PM
As far as I know, you can't get the union or intersection or the difference of the following list with functions
but if you convert them to sets as follows;
Now you can use "and", "or", and minus sign to get their intersection, union, and difference, repsectively;
>>> f and g
set([9, 4, 5, 12])
>>> f or g
set([8, 3, 12, 5, 6])
set([8, 3, 6])
Ok it seems that the Union and Intersections are not correct. "f and g" returns set b and "f or g" returns set a, for some reason!
Last edited by sam_kh918; February 27th, 2006 at 02:29 PM.
Reason: Intersection and Union are not correct!
February 27th, 2006, 12:43 PM
Forgot to mention that, in case you don't know, the main difference b/w a list and a set is the lack of redundancy in sets.
For example if
February 27th, 2006, 01:31 PM
Also, that sets are unordered - that is, when you iterate over them you may get the contents in any order, and you can't do:
- because there is no order, the set doesn't know which elements 0, 1 or 4 are or what is between them.
February 27th, 2006, 02:31 PM
Just wanted to emphasize that the Union and Intersection are not working as I thought, I have edited my previous posting.
February 27th, 2006, 05:16 PM
there you have intersection, union, set difference (both ways) and symmetric difference.
>>> import sets
>>> set1 = sets.Set([1,2,3,4])
>>> set2 = sets.Set([3,4,5,6])
>>> set1 & set2
>>> set1 | set2
Set([1, 2, 3, 4, 5, 6])
>>> set1 - set2
>>> set2 - set1
>>> set1 ^ set2
Set([1, 2, 5, 6])
oh, and just so you know. what you were doing before using "and" and "or" is a logical "and" (&&) and a logical "or" (||). it returns the last object it needed to evaluate in order to determine the value of the logical statement. in the case of the "and" it returns the second one because both need to be true for a true. for the "or" it returns the first one because it is true and automatically makes the statement true.
Comments on this post
February 27th, 2006, 06:27 PM
Thansk alot for your help, that was very helpful.