Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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 将两个numpy数组合并到元组列表中_Python_Numpy - Fatal编程技术网

Python 将两个numpy数组合并到元组列表中

Python 将两个numpy数组合并到元组列表中,python,numpy,Python,Numpy,我还没弄明白。感谢您的帮助: 拥有: 想要: 如果要遍历每个矩阵的行,可以执行以下操作: for (row1, row2) in zip(x,y): yield [tuple(row1), tuple(row2)] # [ (1,2) , (3,4) ] 这将为您提供一个生成器(如果您将其包装在函数中),但您需要一个列表。因此,相反,请用理解来概括: [ [tuple(row1),tuple(row2)] for (row1, row2) in zip

我还没弄明白。感谢您的帮助:

拥有:

想要:


如果要遍历每个矩阵的行,可以执行以下操作:

for (row1, row2) in zip(x,y):
    yield [tuple(row1), tuple(row2)]
       #  [ (1,2)     ,  (3,4)     ]
这将为您提供一个生成器(如果您将其包装在函数中),但您需要一个列表。因此,相反,请用理解来概括:

[ [tuple(row1),tuple(row2)] for (row1, row2) in zip(x,y) ]
试试这个:

x_z = map(tuple,x)
y_z = map(tuple,y)
[list(i) for i in zip(x_z, y_z)]
输出:

[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
IIUC


值得一提的是,以下是最简单、最有效的解决方案(但可能是最不通用的):

result = [[(x[0,0], x[0,1]), (y[0,0], y[0,1])],
         [(x[1,0], x[1,1]), (y[1,0], y[1,1])]]
输出:

[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
[[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
诚然,它不是泛化,但问题是——泛化需要向哪个方向发展?更长的外部尺寸?更长的内部尺寸?这个问题不需要任何概括


根据明确规定的要求,当然可以修改此解决方案,使其尽可能地通用化,这是一个有趣的问题。以下是我的想法:

print([list(map(tuple, i)) for i in zip(x, y)])
# [[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
基本上,压缩x和y可以让您:

[(array([1, 2]), array([3, 4])), (array([5, 6]), array([7, 8])]

因此,首先将每个元素转换为一个列表,然后转换为一个元组

您应该指定
yield
版本需要位于函数定义中可能重复的Thank,但这会生成列表的元组列表,而不是请求的元组列表。Thank,这看起来是最简单的解决方案。
print([list(map(tuple, i)) for i in zip(x, y)])
# [[(1, 2), (3, 4)], [(5, 6), (7, 8)]]
[(array([1, 2]), array([3, 4])), (array([5, 6]), array([7, 8])]