假设以下numpy数组:
[1,2,3,1,2,3,1,2,3,1,2,2]
我想count([1,2])
一次计算所有出现的1和2,得出类似
[4, 5]
对应于[1, 2]
输入。
它在numpy中受支持吗?
答案 0 :(得分:1)
# Setting your input to an array
array = np.array([1,2,3,1,2,3,1,2,3,1,2,2])
# Find the unique elements and get their counts
unique, counts = np.unique(array, return_counts=True)
# Setting the numbers to get counts for as an array
search = np.array([1, 2])
# Gets the counts for the elements in search
search_counts = [counts[i] for i, x in enumerate(unique) if x in search]
这将输出[4,5]