python - 获取列表中给定项目的计数(不使用 numpy)

标签 python list numpy frequency

我试图弄清楚如何计算下面数组中每个唯一数字的频率,范围从 1 - 17:

x3 = [ 8 12 10  9  6  6  5 11  9 10  4 12 10  7  7  7  7  9  8  7  9  6  7  8
  8 10  7  9  9  5  9  7  5 12 12 10  7  6  9  8  6  8  5 11  7  9  7  9
  9  7 12 10 12 11  9 11  6  7  5  8  8  7 10  8  7  9  6 10  6 13]

使用 numpy,这是通过 [np.equal(x3, i).sum() for i in range (1,18)] 完成的

在没有 numpy 的情况下执行此操作的最佳替代方法是什么?

最佳答案

标准集合模块可以做到

import collections
x3 = [1,1,2,2]
counts = collections.Counter(x3)
print(counts)

关于python - 获取列表中给定项目的计数(不使用 numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64345502/

相关文章:

python - 外积并通过向量运算展平 - numpy

python - 用户定义的异常的好名字是什么?

python - 格式化数据的快速查询

java - 我想在 java 中的 Selenium 中创建一个可以返回 List<WebElement> 的方法,我该怎么做?

c# - 避免操作 List 的元素

python - 如何在 TensorFlow 中进行 Argsort?

python - .flat 在 numpy 中的意义是什么?

python - 在 pandas 数据框列中的某个单词处拆分文本

python - 日期列和整数列之间的 Seaborn 热图

r - 确定R中的嵌套级别?