Steven,
Suggestion: It is a bad idea to name any variable "map". When you do, you destroy your ability to call Python's map function. Same goes for "list", "str", or any other built-in function.
If you haven't been bitten by this you will, I was.
Larry Bates
Steven Bethard wrote:
So I end up writing code like this a fair bit:
map = {} for key, value in sequence: map.setdefault(key, []).append(value)
This code basically constructs a one-to-many mapping -- each value that a key occurs with is stored in the list for that key.
This code's fine, and seems pretty simple, but thanks to generator expressions, I'm getting kinda spoiled. ;) I like being able to do something like the following for one-to-one mappings:
dict(sequence)
or a more likely scenario for me:
dict((get_key(item), get_value(item) for item in sequence)
The point here is that there's a simple sequence or GE that I can throw to the dict constructor that spits out a dict with my one-to-one mapping.
Is there a similar expression form that would spit out my one-to-many mapping?
Steve
-- http://mail.python.org/mailman/listinfo/python-list