Reputation: 35
I am trying to process the results of a numpy.unique calculation based on an expected set of unique values - the code below demonstrates what I want. Essentially, I want to have values of 0 when an expected unique value is not found.
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]
'''
Upvotes: 1
Views: 81
Reputation: 2103
This will work -
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 ]
Upvotes: 1