![](/img/trans.png)
[英]Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list
[英]transpose dictionary (extract all the values for one key from a list of dictionaries)
我有一个这样的词典列表:
data = [{'x': 1, 'y': 10},
{'x': 3, 'y': 15},
{'x': 2, 'y': 1},
... ]
我有一个函数(例如matplotlib.axis.plot
),它需要x
和y
值的列表。 所以我必须“转置”字典“。
第一个问题:你怎么称呼这个操作? “转置”正确的术语吗?
我试过这个,但我正在寻找一种有效的方式(也许有一些特殊的numpy
功能):
x = range(100)
y = reversed(range(100))
d = [dict((('x',xx), ('y', yy))) for (xx, yy) in zip(x,y)]
# d is [{'y': 99, 'x': 0}, {'y': 98, 'x': 1}, ... ]
timeit.Timer("[dd['x'] for dd in d]", "from __main__ import d").timeit()
# 6.803985118865967
from operator import itemgetter
timeit.Timer("map(itemgetter('x'), d)", "from __main__ import d, itemgetter").timeit()
# 7.322326898574829
timeit.Timer("map(f, d)", "from __main__ import d, itemgetter; f=itemgetter('x')").timeit()
# 7.098556041717529
# quite dangerous
timeit.Timer("[dd.values()[1] for dd in d]", "from __main__ import d").timeit()
# 19.358459949493408
有更好的解决方案吗? 我的疑问是:在这些情况下,每次重新计算字符串'x'
的哈希值?
从这个答案窃取表格
import timeit
from operator import itemgetter
from itertools import imap
x = range(100)
y = reversed(range(100))
d = [dict((('x',xx), ('y', yy))) for (xx, yy) in zip(x,y)]
# d is [{'y': 99, 'x': 0}, {'y': 98, 'x': 1}, ... ]
D={x:y for x,y in zip(range(10),reversed(range(10)))}
def test_list_comp(d):
return [dd['x'] for dd in d]
def test_list_comp_v2(d):
return [(x["x"], x["y"]) for x in d]
def testD_keys_values(d):
return d.keys()
def test_map(d):
return map(itemgetter('x'), d)
def test_positional(d):
return [dd.values()[1] for dd in d]
def test_lambda(d):
return list(imap(lambda x: x['x'], d))
def test_imap_iter(d):
return list(imap(itemgetter('x'), d))
for test in sorted(globals()):
if test.startswith("test_"):
print "%30s : %s" % (test, timeit.Timer("f(d)",
"from __main__ import %s as f, d" % test).timeit())
for test in sorted(globals()):
if test.startswith("testD_"):
print "%30s : %s" % (test, timeit.Timer("f(D)",
"from __main__ import %s as f, D" % test).timeit())
给出以下结果:
test_imap_iter : 8.98246016151
test_lambda : 15.028239837
test_list_comp : 5.53205787458
test_list_comp_v2 : 12.1928668102
test_map : 6.38402269826
test_positional : 20.2046790578
testD_keys_values : 0.305969839705
显然,最大的胜利是使您的数据格式更接近您已经需要的格式,但您可能无法控制。
就名字而言,我称之为转型。
如果您只需要迭代值,可以考虑以下方法:
imap(lambda x: x['x'], d)
为什么不这样的?
[(x["x"], x["y"]) for x in d]
这将返回包含x和y位置的元组列表。 我不确定它的速度,但它会摆脱lambda开销。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.