Python 列表到元组

标签 python arrays

我有这个:

(([75, 0], [100, 0], [100, 370]), ([75, 0], [100, 370], [75, 370])) 

来自于此:

 [(array([75,  0]), array([100,   0]), array([100, 370])), (array([75,  0]), array([100, 370]), array([ 75, 370]))]

我想要:

[(x1, y1, x2 , y2 ,x3 ,y3), (x1, y1, x2 , y2 ,x3 ,y3), ...] 

[(75, 0, 100, 0, 100, 370), (75, 0, 100, 0, 100, 370),.....]

感谢您的帮助!

最佳答案

您可以使用itertools.chain:

import itertools
s = (([75, 0], [100, 0], [100, 370]), ([75, 0], [100, 370], [75, 370])) 
final_s = [list(itertools.chain.from_iterable(i)) for i in s]

输出:

[[75, 0, 100, 0, 100, 370], [75, 0, 100, 370, 75, 370]]

或者在Python2中使用reduce:

s = (([75, 0], [100, 0], [100, 370]), ([75, 0], [100, 370], [75, 370]))    
new_s = [reduce(lambda x, y:list(x)+list(y), i) for i in s]

输出:

[[75, 0, 100, 0, 100, 370], [75, 0, 100, 370, 75, 370]]

关于Python 列表到元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613323/

相关文章:

arrays - 如何将列表框中的所有选定数据存储在VBA中的数组中

python - Python 编程的新手,有人可以解释一下这个程序的错误吗?

c++ - 结构未完成排序

c - 将数组传递给函数时使用的正确模式

c - 在 C 中为两个(一维)数组赋值

java - 必须是数组类型但解析为字符串

python - 我应该如何导入类模块以对其进行测试

Python 安装工具 : how to call function on import but not when run from script?

python - FormView不更新但创建模型的新记录

python - OpenCV 如何对矩形进行分组