元组数组:根据权重返回唯一值(Python)



我已经在Python中压缩了两个数组

w
array([ 0.5 ,  1.  ,  0.5 ,  1.  ,  1.  ,  1.  ,  0.75,  1.  ])
index
array([ 218,  218, 1491, 2456, 1491, 1490,  250,  219])
test=zip(w,index)
test
[(0.5, 218), (1.0, 218), (0.5, 1491), (1.0, 2456), (1.0, 1491), (1.0, 1490), (0.75, 250), (1.0, 219)]

,我想返回一个新的元组列表,它只包含唯一的索引(即。"指数"),它们的权重最高。

换句话说,在这种情况下,我想获得:

test2
[(1.0, 218), (1.0, 2456), (1.0, 1491), (1.0, 1490), (0.75, 250), (1.0, 219)]

任何想法?

d = {}
# Group the weights based on the indices
for wt, idx in test:
    d.setdefault(idx, []).append(wt)
# Create a new list with the max of weights and the index tuples
print [(max(d[idx]), idx) for idx in d]
# [(1.0, 1490), (1.0, 1491), (1.0, 2456), (0.75, 250), (1.0, 219), (1.0, 218)]

最新更新