#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14

    Union and Intersection of lists


    Hello,

    Are there functions for obtaining the union and intersection of two lists or does it have to be coded?

    Thanks for your help.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    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!
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by sam_kh918
    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?
    If by redundant you mean non-unique elements then yes.

    grim
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Lisbon, Portugal
    Posts
    154
    Rep Power
    12
    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!

    Thanks!
    "Get it hot! Hit it harder!!!"
    --- The tutor warcry

    ActivePython
    Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community!
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    Sure,
    As far as I know, you can't get the union or intersection or the difference of the following list with functions
    Code:
    >>> a=[3,5,6,8,12]
    >>> b=[4,5,9,12]
    but if you convert them to sets as follows;
    Code:
    >>> f=set(a)
    >>> g=set(b)
    Now you can use "and", "or", and minus sign to get their intersection, union, and difference, repsectively;
    Code:
    >>> f and g
    set([9, 4, 5, 12])
    >>> f or g
    set([8, 3, 12, 5, 6])
    >>> f-g
    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!
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    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
    a=[3,4,6,8,8,8,12,14,14]
    then
    set(a)=[3,4,6,8,12,14]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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:

    Code:
    print myset[0]
    
    or
    
    myset[1:4]
    - because there is no order, the set doesn't know which elements 0, 1 or 4 are or what is between them.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    Just wanted to emphasize that the Union and Intersection are not working as I thought, I have edited my previous posting.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    53
    Rep Power
    11
    Code:
    >>> import sets
    >>> set1 = sets.Set([1,2,3,4])
    >>> set2 = sets.Set([3,4,5,6])
    >>> set1 & set2
    Set([3, 4])
    >>> set1 | set2
    Set([1, 2, 3, 4, 5, 6])
    >>> set1 - set2
    Set([1, 2])
    >>> set2 - set1
    Set([5, 6])
    >>> set1 ^ set2
    Set([1, 2, 5, 6])
    there you have intersection, union, set difference (both ways) and symmetric difference.

    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

    • CountVikernes agrees : Just like = being assignment and == being comparison! Good one, thanks!
    • netytan agrees : Finally some code using sets in a set thread :)
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    Thansk alot for your help, that was very helpful.

IMN logo majestic logo threadwatch logo seochat tools logo