Numpy独特的预期唯一值

时间:2018-03-28 16:08:23

标签: python numpy

我正在尝试根据预期的一组唯一值处理numpy.unique计算的结果 - 下面的代码演示了我想要的内容。基本上,当未找到预期的唯一值时,我希望值为0.

import numpy

unqVals = [1,2,3,4,5,6]

x = [1,1,1,2,2,2,3,3,4,4,6,6]
y = [1,1,2,2,3,3,4,4,5,5,6,6]
z = [1,1,2,2,2,3,3,3,3,4,4,5]

for cur in [x,y,z]:
    xx = numpy.unique(cur, return_counts=True)
    print xx[1]

''' Current Results
[3 3 2 2 2]
[2 2 2 2 2 2]
[2 3 4 2 1]

Desired Results - based on the unqVals definition 
[3 3 2 2 0 2]
[2 2 2 2 2 2]
[2 3 4 2 1 0]
'''

1 个答案:

答案 0 :(得分:1)

这将有效 -

from collections import Counter

unqVals = [1,2,3,4,5,6]

x = [1,1,1,2,2,2,3,3,4,4,6,6]
y = [1,1,2,2,3,3,4,4,5,5,6,6]
z = [1,1,2,2,2,3,3,3,3,4,4,5]

for cur in [x,y,z]:
    xx = dict(Counter(cur))
    print [xx.get(i, 0) for i in unqVals ]