Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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,我有一个数据列表: [[], [], [(2, 3), (2, 7), (3, 2), (7, 2)], [(2, 3), (3, 2), (3, 7), (7, 3)], [], [], [], [(2, 7), (3, 7), (7, 2), (7, 3)], [], []] 我想删除元组中的括号,这样[(2,3)、(2,7)、(3,2)、(7,2)]将变成(2,3,2,7,3,2,7,2) 有没有快捷方式可以做到这一点?您正在查看一个列表。在python中,您可以使用复合列表来实现这一点

我有一个数据列表:

[[], [], [(2, 3), (2, 7), (3, 2), (7, 2)], [(2, 3), (3, 2), (3, 7), (7, 3)], [], [], [], [(2, 7), (3, 7), (7, 2), (7, 3)], [], []]
我想删除元组中的括号,这样
[(2,3)、(2,7)、(3,2)、(7,2)]
将变成
(2,3,2,7,3,2,7,2)


有没有快捷方式可以做到这一点?

您正在查看一个列表。在python中,您可以使用复合列表来实现这一点

data = [(2, 3), (3, 2), (3, 7), (7, 3)]
flattend = [item for lst in data for item in lst]
您可以使用:


这不管用。这将返回一个元组列表,这些元组与OP想要的不匹配。
[tuple(itertools.chain(*el))如果el else el for el in l]
我认为不应该将空列表转换为tuple对象。@TanveerAlam:我也这么认为。然而,我不想猜测OP的需求(在这方面是不明确的)。感谢
[tuple(np.array(ls.flatte())for ls in l]
也给出了相同的结果。我认为dupe close不正确。OP似乎想保留最外层的列表,而只需打开内部列表。NPE在他的回答中有效地回答了这个问题。
In [6]: l = [[], [], [(2, 3), (2, 7), (3, 2), (7, 2)], [(2, 3), (3, 2), (3, 7), (7, 3)], [], [], [], [(2, 7), (3, 7), (7, 2), (7, 3)], [], []]

In [7]: [tuple(itertools.chain(*el)) for el in l]
Out[7]: 
[(),
 (),
 (2, 3, 2, 7, 3, 2, 7, 2),
 (2, 3, 3, 2, 3, 7, 7, 3),
 (),
 (),
 (),
 (2, 7, 3, 7, 7, 2, 7, 3),
 (),
 ()]