python转置列表代码_转置字典(从字典列表中提取一个键的所有值)

从这个answer窃取表单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())

得出以下结果:

^{pr2}$

显然,最大的成功是使数据的格式更接近您已经需要的格式,但您可能无法控制这一点。在

就名字而言,我称之为转变。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值