可以說我有一個簡單的數組:如何矩陣不等長的項目組值
a = np.arange(3)
和指數的相同長度的數組:
I = np.array([0, 0, 1])
我現在想根據指數對值進行分組。 如何將第一個數組的元素分組以產生下面的結果?
np.array([[0, 1], [2], dtype=object)
這裏是我的嘗試:
a = np.arange(3)
I = np.array([0, 0, 1])
out = np.empty(2, dtype=object)
out.fill([])
aslists = np.vectorize(lambda x: [x], otypes=['object'])
out[I] += aslists(a)
然而,這種方法沒有並置的名單,但只維持每個索引的最後一個值:
array([[1], [2]], dtype=object)
或者,對於2維情況:
a = np.random.rand(100)
I = (np.random.random(100) * 5 //1).astype(int)
J = (np.random.random(100) * 5 //1).astype(int)
out = np.empty((5, 5), dtype=object)
out.fill([])
如何根據兩個索引數組附加項目?
目前它不是,但我可以排序,是 –