在使用獨特的 numpy 函式后,我正在尋找組資料的解決方案。我認為一個例子更好:
>>> t
[[0, 3, 4], [1, 2, 8], [1, 2, 8]] #array of multiples values
>>> ids = ['A', 'B', 'C'] #Ids associated with previous values
>>> np.unique(t, axis=0)
array([[0, 3, 4],
[1, 2, 8]]) #Result of unique (so 2 rows ofc)
>>> array([['A'],
['B', 'C']]) #What i want to got (and generated with numpy ideally)
非常感謝您的幫助。
uj5u.com熱心網友回復:
也許你可以創建一個字典,其中鍵是來自元素的元組t,值是來自的字母ids
d = {}
for i in range(len(ids)):
d.setdefault(tuple(t[i]), []).append(ids[i])
d
# {(0, 3, 4): ['A'], (1, 2, 8): ['B', 'C']}
uj5u.com熱心網友回復:
一位朋友找到了一個很好的方法來做到這一點。(僅在資料已排序時才有效)
import numpy as np
t = np.array([[0, 3, 4], [1, 2, 8], [1, 2, 8]])
ids = np.array(['A', 'B', 'C'])
print(t)
res = np.split(ids, np.unique(t, return_index=True, axis=0)[1][1:])
print(res) # [array(['A'], dtype='<U1'), array(['B', 'C'], dtype='<U1')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370331.html
