name_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}
for row in rows:
# Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
row = dict((name_map[name], val) for name, val in row.iteritems())
...
Or in Python2.7+ with Dict Comprehensions:
for row in rows:
row = {name_map[name]: val for name, val in row.items()}
Answer from elo80ka on Stack Overflowname_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}
for row in rows:
# Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
row = dict((name_map[name], val) for name, val in row.iteritems())
...
Or in Python2.7+ with Dict Comprehensions:
for row in rows:
row = {name_map[name]: val for name, val in row.items()}
If you are using Python 2.7 or Python 3.x, you can use a dictionary comprehension. This is equivalent elo80ka's answer (which used a list comprehension), but produces slightly more readable code.
name_map = {'oldcol1': 'newcol1', 'oldcol2': 'newcol2', 'oldcol3': 'newcol3'...}
for row in rows:
# Each row is a dict of the form: {'oldcol1': '...', 'oldcol2': '...'}
row = {name_map[name]: val for name, val in row.iteritems()}
...
Mapping over values in a python dictionary - Stack Overflow
Python dictionary: mapping multiple keys to a unique value?
How can I map keys to multiple values in a dictionary? (multidict) here's my code:
Python map() dictionary values - Stack Overflow
» pip install map-dictionary-keys
There is no such function; the easiest way to do this is to use a dict comprehension:
my_dictionary = {k: f(v) for k, v in my_dictionary.items()}
Note that there is no such method on lists either; you'd have to use a list comprehension or the map() function.
As such, you could use the map() function for processing your dict as well:
my_dictionary = dict(map(lambda kv: (kv[0], f(kv[1])), my_dictionary.items()))
but that's not that readable, really.
(Note that if you're still using Python 2.7, you should use the .iteritems() method instead of .items() to save memory. Also, the dict comprehension syntax wasn't introduced until Python 2.7.)
These toolz are great for this kind of simple yet repetitive logic.
http://toolz.readthedocs.org/en/latest/api.html#toolz.dicttoolz.valmap
Gets you right where you want to be.
import toolz
def f(x):
return x+1
toolz.valmap(f, my_list)
Is it possible to map multiple keys to a unique value in python? Like could I have the numbers 0,1,2 map to 0, and the numbers 3,4 map to 1, and so on? Thanks!
~ Shoot for the Stars ~
# Mapping Keys to Multiple Values in a Dictionary (multidict)
from collections import defaultdict
first_dict = {'a': 1, 'b': 2, 'c': 3}
second_dict = defaultdict(list)
for key, value in first_dict.items():
second_dict[key].append(value)
print(second_dict)
# defaultdict(<type 'list'>, {'a': [1]})
# defaultdict(<type 'list'>, {'a': [1], 'c': [3]})
# defaultdict(<type 'list'>, {'a': [1], 'c': [3], 'b': [2]})In Python 3, map returns an iterator, not a list. You still have to iterate over it, either by calling list on it explicitly, or by putting it in a for loop. But you shouldn't use map this way anyway. map is really for collecting return values into an iterable or sequence. Since neither print nor set.update returns a value, using map in this case isn't idiomatic.
Your goal is to put all the keys in all the counters in counters into a single set. One way to do that is to use a nested generator expression:
s = set(key for counter in counters.values() for key in counter)
There's also the lovely dict comprehension syntax, which is available in Python 2.7 and higher (thanks Lattyware!) and can generate sets as well as dictionaries:
s = {key for counter in counters.values() for key in counter}
These are both roughly equivalent to the following:
s = set()
for counter in counters.values():
for key in counter:
s.add(key)
You want the set-union of all the values of counters? I.e.,
counters[1].union(counters[2]).union(...).union(counters[n])
? That's just functools.reduce:
import functools
s = functools.reduce(set.union, counters.values())
If counters.values() aren't already sets (e.g., if they're lists), then you should turn them into sets first. You can do it using a dict comprehension using iteritems, which is a little clunky:
>>> counters = {1:[1,2,3], 2:[4], 3:[5,6]}
>>> counters = {k:set(v) for (k,v) in counters.iteritems()}
>>> print counters
{1: set([1, 2, 3]), 2: set([4]), 3: set([5, 6])}
or of course you can do it inline, since you don't care about counters.keys():
>>> counters = {1:[1,2,3], 2:[4], 3:[5,6]}
>>> functools.reduce(set.union, [set(v) for v in counters.values()])
set([1, 2, 3, 4, 5, 6])