假设有以下 numpy 数组:
[1,2,3,1,2,3,1,2,3,1,2,2]
我想要count([1,2])
在一次运行中计算 1 和 2 的所有出现次数,产生类似
[4, 5]
对应于[1, 2]
输入。
numpy 支持吗?
最佳答案
# 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 a set
search = {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]
关于python - 如何计算numpy中多个项目的出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078267/