【发布时间】:2023-03-11 12:59:01
【问题描述】:
我有几个简单的问题无法找到答案。它们都在以下示例代码中进行了说明。感谢您的任何帮助!
import numpy as np
#here are two arrays to join together
a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])
#here comes the joining step I don't know how to do better
#QUESTION 1: How to form all permutations of two 1D arrays?
temp = np.array([]) #empty array to be filled with values
for aa in a:
for bb in b:
temp = np.append(temp,[aa,bb]) #fill the array
#QUESTION 2: Why do I have to reshape? How can I avoid this?
temp = temp.reshape((int(temp.size/2),2))
编辑:使代码更简洁
【问题讨论】:
-
那么,你的循环部分不是实现了形成所有排列的目标吗?
-
是的,但肯定有一种干净的方法可以做到这一点?我试图避免在numpy中循环数组。我可以使用 np.zip() 之类的应用程序吗?
标签: python numpy multidimensional-array append