我有幾個簡單的問題,我無法找到答案。它們都在以下示例代碼中聲明。感謝您的任何幫助!將多維元素添加到numpy數組中而無需重新塑形
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))
編輯:由代碼的最小
那麼,你的loopy部分是不是實現了形成所有排列的目標? – Divakar
是的,但肯定有一個乾淨的方法來做到這一點?我試圖避免在numpy中循環數組。是否有一些像np.zip()我可以使用的應用程序? – kevinkayaks