[Tutor] working with multiple sets

bob gailer bgailer at gmail.com
Sat Sep 5 20:06:05 CEST 2009


1) Please don't hijack a post then change the subject. That screws 
things up for email clients that track threads. I have started a new 
thread with this reply.

2) Be sure to reply-all so a copy goes to the list.

[SNIP]

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.

I think you mean "set" rather than "list"

To enable processing of an arbitrary number of sets, put them in a 
collection (list or dictionary). Use a list if it is sufficient to 
identify sets by number, else use a dictionary.

Use a dictionary to relate items to their set(s).

import collections
lookup = collections.defaultdict(list)
sets = {'x': set((1,2,3)), 'y': set((2,3))}
for key, value in sets.items():
  for element in value:
    lookup[element].append(key)
print lookup


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.
-- 
Bob Gailer
Chapel Hill NC
919-636-4239



More information about the Tutor mailing list