打开列表元组列表

gob*_*s14 4 python tuples list-comprehension list

我有一个元组列表,其中元组中的一个元素是一个列表.

example = [([0, 1, 2], 3, 4), ([5, 6, 7], 8, 9)]
Run Code Online (Sandbox Code Playgroud)

我想最后得到一个元组列表

output = [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9)]
Run Code Online (Sandbox Code Playgroud)

这个问题似乎解决了元组的问题,但我担心因为我的用例在内部列表中有更多的元素

[(a, b, c, d, e) for [a, b, c], d, e in example]
Run Code Online (Sandbox Code Playgroud)

看起来很单调乏味.有没有更好的方法来写这个?

iCo*_*dez 5

元组可以与+类似列表连接.所以,你可以这样做:

>>> example = [([0, 1, 2], 3, 4), ([5, 6, 7], 8, 9)]
>>> [tuple(x[0]) + x[1:] for x in example]
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9)]
Run Code Online (Sandbox Code Playgroud)

请注意,这适用于Python 2.x和3.x.