[Python-ideas] Remapping a table's keys
Andrey Fedorov
anfedorov at gmail.com
Fri Nov 5 02:07:00 CET 2010
Hi guys,
I often run into the problem when I need to systematically rename a bunch of
keys in a table. For example, a JSON API returns an object where I need to
map "Media Cost" to "adjusted_partner_cost", "Impressions" to "bid_count",
etc. One way of doing this is:
key_map = {
'Media Cost': 'adjusted_partner_cost',
'Impressions': 'bids_won',
'Day': 'date'
}
mykeys = {}
for k, v in dict_from_elsewhere.items():
if k in key_map:
ret[key_map[k]] = v
else:
ret[k] = dict_from_elsewhere[k]
I would love to be able to instead say something like:
mykeys = defaultdict(lambda x:x, {
'Media Cost': 'adjusted_partner_cost',
'Impressions': 'bids_won',
'Day': 'date'
})
But default functions don't take parameters. Ideas?
- Andrey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20101104/4eeba7bc/attachment.html>
More information about the Python-ideas
mailing list