kevin parks wrote:
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?
It simplifies coding, as it takes care of initializing each new entry to
a list.
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
--
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