I am looking at this and wondering:
Why does this use collections.defaultdict ?
In fact i guess since collections.defaultdict is new to me i am not
even sure why it exists
and why someone would use this as opposed to using Python's built-in
dictionary? and why was it
used in this instance?
cheers,
-kp--
On Sep 6, 2009, at 3:06 AM, bob gailer wrote:
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
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor