gpt4 book ai didi

python - 沿指定维度连接数组矩阵。 Python

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:45 26 4
gpt4 key购买 nike

如何按不同维度连接 C1 和 C2?

C1 =[[0,0,1,0],
[2,0,0,0],
[0,0,0,0]]

C2=[[0,3,4,0,1,1],
[0,0,0,5,1,1],
[6,7,0,8,1,1]]

如何得到以下结果:

[[0 ,0 ,1 ,0 ,0, 3, 4, 0,1,1]
[2, 0, 0, 0, 0, 0 ,0 ,5,1,1]
[0, 0, 0 ,0 ,6 ,7, 0, 8,1,1]]

[[0,0,1,0],
[2,0,0,0],
[0,0,0,0],
[0,3,4,0,1,1],
[0,0,0,5,1,1],
[6,7,0,8,1,1]]

最佳答案

>>> [r1+r2 for (r1, r2) in zip(C1, C2)]
[[0, 0, 1, 0, 0, 3, 4, 0, 1, 1], [2, 0, 0, 0, 0, 0, 0, 5, 1, 1], [0, 0, 0, 0, 6, 7, 0, 8, 1, 1]]

>>> C1 + C2
[[0, 0, 1, 0], [2, 0, 0, 0], [0, 0, 0, 0], [0, 3, 4, 0, 1, 1], [0, 0, 0, 5, 1, 1], [6, 7, 0, 8, 1, 1]]

关于python - 沿指定维度连接数组矩阵。 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41117605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com