[Tutor] working with multiple sets
kevin parks
kp8 at mac.com
Sat Sep 5 10:18:01 CEST 2009
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.
More information about the Tutor
mailing list