Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何创建元组的合并列表_Python - Fatal编程技术网

Python 如何创建元组的合并列表

Python 如何创建元组的合并列表,python,Python,我有两个元组列表: leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]') ] rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[2

我有两个元组列表:

leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]')   ]

rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[299]')  ]
我想从每个元组中提取第二个元素,并将其放入一个新的元组列表中,如下所示:

merged_list = [('testSphere.vtx[299]', 'testSphere.vtx[289]'), ('testSphere.vtx[298]', 'testSphere.vtx[290]'), ('testSphere.vtx[277]', 'testSphere.vtx[299]') 

最好使用列表理解还是for循环

使用zip和地图/列表理解:

zip((x[1] for x in leftlist),(y[1] for y in rightlist))
zip(map(lambda x : x[1],leftlist),map(lambda y: y[1],rightlist))

两者最终都会得到相同的结果

使用zip和地图/列表理解:

zip((x[1] for x in leftlist),(y[1] for y in rightlist))
zip(map(lambda x : x[1],leftlist),map(lambda y: y[1],rightlist))

两者最终都会得到相同的结果

下面是一个列表:

>>> leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]')   ]
>>> rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[299]')  ]
>>> result = [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
>>> result
[(u'testSphere.vtx[299]', u'testSphere.vtx[289]'), (u'testSphere.vtx[298]', u'testSphere.vtx[290]'), (u'testSphere.vtx[277]', u'testSphere.vtx[299]')]
只是我注意到这和一小时前删除的答案是一样的。如果@struchtmaster重新给出了答案,请将此归功于@struchtmaster


后来:

如果您希望代码速度更快(即使不那么直观),这很好:

result = [(x, y) for (_, x), (_, y) in zip(leftlist, rightlist)]
如果您喜欢这种微优化,您可以在此处看到计时结果:

In [4]: %timeit [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)]
1000000 loops, best of 3: 680 ns per loop

In [5]: %timeit [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
1000000 loops, best of 3: 787 ns per loop

In [6]: %timeit [(x, y) for (_, x), (_, y) in zip(leftlist, rightlist)]
1000000 loops, best of 3: 630 ns per loop

以下是一份清单:

>>> leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]')   ]
>>> rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[299]')  ]
>>> result = [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
>>> result
[(u'testSphere.vtx[299]', u'testSphere.vtx[289]'), (u'testSphere.vtx[298]', u'testSphere.vtx[290]'), (u'testSphere.vtx[277]', u'testSphere.vtx[299]')]
只是我注意到这和一小时前删除的答案是一样的。如果@struchtmaster重新给出了答案,请将此归功于@struchtmaster


后来:

如果您希望代码速度更快(即使不那么直观),这很好:

result = [(x, y) for (_, x), (_, y) in zip(leftlist, rightlist)]
如果您喜欢这种微优化,您可以在此处看到计时结果:

In [4]: %timeit [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)]
1000000 loops, best of 3: 680 ns per loop

In [5]: %timeit [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
1000000 loops, best of 3: 787 ns per loop

In [6]: %timeit [(x, y) for (_, x), (_, y) in zip(leftlist, rightlist)]
1000000 loops, best of 3: 630 ns per loop

您还可以使用enumerate,它似乎是最有效的

leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]')   ]

rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[299]')  ]

In [12]: [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)]
Out[12]: 
[(u'testSphere.vtx[299]', u'testSphere.vtx[289]'(u'testSphere.vtx[298]',u'testSphere.vtx[290]'),(u'testSphere.vtx[277]', u'testSphere.vtx[299]')]





In [7]: %timeit [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)
]1000000 loops, best of 3: 849 ns per loop

In [8]: %timeit  [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
1000000 loops, best of 3: 951 ns per loop

In [9]: %timeit zip(map(lambda x : x[1],leftlist),map(lambda y: y[1],rightlist))
100000 loops, best of 3: 1.86 µs per loop

In [10]: %timeit zip((x[1] for x in leftlist),(y[1] for y in rightlist))
100000 loops, best of 3: 2.11 µs per loop

您还可以使用enumerate,它似乎是最有效的

leftlist = [(1.52, u'testSphere.vtx[299]'), (1.4450000000000001, u'testSphere.vtx[298]'), (1.0, u'testSphere.vtx[277]')   ]

rightlist = [(1.52, u'testSphere.vtx[289]'), (1.4450000000000001, u'testSphere.vtx[290]'), (1.1, u'testSphere.vtx[299]')  ]

In [12]: [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)]
Out[12]: 
[(u'testSphere.vtx[299]', u'testSphere.vtx[289]'(u'testSphere.vtx[298]',u'testSphere.vtx[290]'),(u'testSphere.vtx[277]', u'testSphere.vtx[299]')]





In [7]: %timeit [(leftlist[i][1],j[1]) for i,j in enumerate(rightlist)
]1000000 loops, best of 3: 849 ns per loop

In [8]: %timeit  [(x[1], y[1]) for x, y in zip(leftlist, rightlist)]
1000000 loops, best of 3: 951 ns per loop

In [9]: %timeit zip(map(lambda x : x[1],leftlist),map(lambda y: y[1],rightlist))
100000 loops, best of 3: 1.86 µs per loop

In [10]: %timeit zip((x[1] for x in leftlist),(y[1] for y in rightlist))
100000 loops, best of 3: 2.11 µs per loop

zip([x[1]表示左列表中的x],[x[1]表示右列表中的x])
zip([x[1]表示左列表中的x],[x[1]表示右列表中的x])
?+1用于记住在第一行末尾使用生成器表达式而不是列表理解u missesd)+1用于记住在第一行末尾使用生成器表达式而不是列表理解u missesd)