[Tutor] working with multiple sets

Dave Angel davea at ieee.org
Sat Sep 5 19:09:59 CEST 2009


kevin parks wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">I am 
> doing some simple things with sets and so far have had a lot of 
> success with python's built-in sets, which is such a great new(ish) 
> "batteries included" type python data type.
>
> -------- [snip] -------- [snip] -------------- [snip] -------- [snip] 
> ------
> #!/usr/bin/env python
>
> def test():
>     x = range(10)
>     y = range(5, 15)
>     z = range(8, 22)
>     setx = set(x)
>     sety = set(y)
>     setz = set(z)
>     print "\n", "x = ", x, "\n", "y = ", y, "\n", "z = ", z, "\n" * 2
>     overlap1 = setx.intersection(sety)
>     overlap1 = sorted(list(overlap1))
>     print "overlap of x and y:", overlap1
>     #
>     overlap2 = sety.intersection(setz)
>     overlap2 = sorted(list(overlap2))
>     print "overlap of y and z:", overlap2
>     #
>     overlap3 = setx.intersection(setz)
>     overlap3 = sorted(list(overlap3))
>     print "overlap of x and z:", overlap3
>     
> if __name__ == "__main__":
>     test()
>
> -------- [snip] -------- [snip] -------------- [snip] -------- [snip] 
> --------------
>
>
> so silly stuff like that works fine. But i want to do a little more 
> than that. I want to be able to look at a number/item and see which 
> lists it is in so that i could maybe have a master list of all the 
> data, a superset, and then an indication of which lists that data  was 
> in, as some items will only be in one list, some will appear in two 
> lists (x & y, or x & z or y & z) and a small handful will be in all 
> three lists.
>
> 0 - x
> 1 - x
> 2 - x
> 3 - x
> 4 - x
> 5 - x, y
> 6 - x, y
> 7 - x, y
> 8 - x, y, z
> 9 - x, y, z
> 10 - y, x
>
> etc.
>
> Of course the whole point of this is that the sets will be more 
> complicated than 0-9, 5-14, and 8-21 and additionally, the sets may 
> not be a list of numbers but eventually a list of strings.
>
> So the steps would be to create the superset
> then test for membership for each list?
>
> I kinda get it, the thing that warps my brain is the idea that there 
> are more than 2 lists now to test against.... eventually my script 
> needs to accommodate 4, 5, 6 sets.. but i would just like to see if i 
> can get 3 sets to work first.
>
>
> </div>
>
The real question is why bother to make up a new data structure.  If 
you're not careful you'll end up with multiple copies of the same data, 
and it'll be a pain to keep them in synch.  Perhaps all you need is a 
list of sets, and one or more functions to query that list.

You'll find that checking an item for membership in a set is very quick, 
so making a separate set of lists (that tells you which ones a given 
item is in) is probably not productive.

x = range(10)
y = range(5, 15)
z = range(8, 22)
mysets = [(x,"set x"), (y, "set y"), (z, "set z")]      #any number of them

def find_item(item, mysets):
    members = []
    for myset, mysetname in mysets:
        if item in myset:
           members.append(mysetname)
    return members


print find_item(4, mysets), find_item(5, mysets), find_item(9, mysets), 
find_item(12, mysets), find_item(21, mysets)

DaveA



More information about the Tutor mailing list