将元组列表转换为多个列表 [英] convert list of tuples into several lists

查看:114
本文介绍了将元组列表转换为多个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我可以找到将列表转换为元组列表的示例,例如:

Hi,

I can find examples to convert lists into a list of tuples like:

map(无,[1,2,3],[4,5,6])
map(None,[1,2,3],[4,5,6])



[ (1,4),(2,5),(3,6)]


但是怎么做反之亦然?


奥利弗


[(1,4),(2,5),(3,6)]

but how is it done vice versa?

Oliver

推荐答案

zip(* [(1,4),(2,5),(3,6)])


-

问候,


Diez B. Roggisch
zip(*[(1,4),(2,5),(3,6)])

--
Regards,

Diez B. Roggisch


Diez B. Roggisch写道:
Diez B. Roggisch wrote:
zip(* [(1,4),(2,5),(3,6)])
zip(*[(1,4),(2,5),(3,6)])



谢谢:)我知道它一定很简单。星号 - 对我来说是新事物。


顺便问一下:什么更快?


这个:

z = [(1,4),(2,5),(3,6)

a,b = zip(* [(x [0],x [0] - x [1])for x in z])


或:


a = []

b = [ ]

for x in z:

a.append(x [0])

b.append(x [0] -x [ 1])


我想是第一个,不是吗?

Oliver


Thanks :) I knew it must be simple. The asterics - thing was new to me.

By the way: What is faster?

this:

z = [(1,4),(2,5),(3,6)
a,b = zip(*[(x[0], x[0]-x[1]) for x in z])

or:

a = []
b = []
for x in z:
a.append(x[0])
b.append(x[0]-x[1])

I guess first, isn''t it?
Oliver


Oliver Eichleraécrit:
Oliver Eichler a écrit :
Diez B. Roggisch写道:

Diez B. Roggisch wrote:

zip(* [(1,4),(2,5), (3,6)])
zip(*[(1,4),(2,5),(3,6)])



谢谢:)我知道它一定很简单。星号 - 对我来说是新事物。

顺便说一句:什么更快?

这个:

z = [(1,4 ),(2,5),(3,6)
a,b = zip(* [(x [0],x [0] -x [1])for x in z])


a = []
b = []
对于x中的x:
a.append(x [0])
b.append(x [0] -x [1])

我想是第一个,不是吗?

Oliver



Thanks :) I knew it must be simple. The asterics - thing was new to me.

By the way: What is faster?

this:

z = [(1,4),(2,5),(3,6)
a,b = zip(*[(x[0], x[0]-x[1]) for x in z])

or:

a = []
b = []
for x in z:
a.append(x[0])
b.append(x[0]-x[1])

I guess first, isn''t it?
Oliver




最佳答案是:试试吧:)

使用timeit模块(在标准库中)这样做...


Pierre



Best answer is : try it :)
use the "timeit" module (in the standard lib) to do so ...

Pierre


这篇关于将元组列表转换为多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆