Python 统计列表里面有多少个元素 python 如何统计dataframe中某一列数据中每个元...

\u5982\u4f55\u7edf\u8ba1python list\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u53ca\u5176\u4f4d\u7f6e

\u4ee3\u7801\u5982\u4e0b\uff1alist1=[2,3,1,9,112,23,23,6,6,1,3,0,11,2,1,1]L1=len(list1)
#\u5217\u8868list1\u7684\u957f\u5ea6list2=list(set(list1))
#\u53ef\u4ee5\u7528set\uff0c\u76f4\u63a5\u53bb\u6389\u91cd\u590d\u7684\u5143\u7d20list2.sort(reverse=False)
#\u5c06\u5217\u8868\u7531\u5c0f\u5230\u5927\u6392\u5e8fL2=len(list2)
#\u5217\u8868list2\u7684\u957f\u5ea6print('\u96c6\u5408:'+str(list1))for m in range(L2): X=set()
#\u8bbe\u5b9a\u4e00\u4e2a\u7a7a\u7684\u96c6\u5408\uff0c\u7528\u6765\u5b58\u653e\u8fd9\u4e2a\u5143\u7d20\u7684\u6240\u5728\u7684\u4f4d\u7f6e start=list1.index(list2[m])
for n in range(L1):
stop=L1
if list2[m] in tuple(list1)[start:stop]:
a=list1.index(list2[m],start,stop)
X.add(a) start=start+1 print('\u5143\u7d20\uff1a'+str(list2[m])+'\uff0c
\u4e00\u5171\u6709'+str(len(X))+'\u4e2a\uff0c\u5728\u5217\u8868\u4f4d\u7f6e\u96c6\u5408\u4e3a\uff1a'+str(X))\u3002

\u4e0d\u63a8\u8350\u4f7f\u7528collections\u7edf\u8ba1\u6216\u8005list.count\u6765\u7edf\u8ba1\uff0c\u56e0\u4e3a\u53ef\u80fd\u4f1a\u9047\u5230TypeError: unhashable type: 'list\u2019\u9519\u8bef\u3002
\u6b64\u5916\u4e5f\u4e0d\u63a8\u8350\u4f7f\u7528df3[\u201cAlarm_Z\u201d].value_counts()\u6765\u7edf\u8ba1\uff0c\u56e0\u4e3a\u7248\u672c\u539f\u56e0\uff0c\u6709\u4e9b\u7248\u672c\u7684pandas\u597d\u50cf\u6ca1\u6709\u8fd9\u4e2a\u65b9\u6cd5\u3002
\u6ce8\u610f\uff0c\u5f53\u5217\u8868\u4e2d\u542b\u6709\u7f3a\u5931\u503c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5931\u6548\uff0c\u9700\u8981\u5148\u7528\u5b57\u7b26\u578b\u7684\u201cnan\u201d\u6765\u586b\u5145\u7f3a\u5931\u503c\u3002

Python 统计列表里面有多少个元素步骤如下:

1、打开python语言命令窗口,定义一个列表变量Z并打印对应的列表值。

2、调用列表中的方法max(),可以获取列表中所有元素中最大值。

3、使用第二步中对应的方法min(),可以获取列表中最小值。

4、如果想要获取列表中所有元素的个数,使用count()方法,结果出现了报错,然后使用len()方法。

5、再次调用count方法,不过这次需要传一个参数,查询列表中某个元素是否在其中。

6、最后在列表中,使用方法pop进行退出元素,然后返回这个元素。



可以使用collections模块下的Counter类来进行统计,代码如下:

from collections import Counter

arr = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'c', 'd', 'f', 'g']

result = Counter(arr)

print(result)

运行程序后,输出结果:Counter({'a': 3, 'c': 3, 'b': 2, 'd': 1, 'g': 1, 'f': 1})。

结果以字典key-value形式直接显示出a、b、c、d等所有元素出现的个数。key是出现的元素,value是元素出现的个数。


扩展资料

python中Counter库的介绍:

collections 包下的 Counter 也是一个很有用的工具类,它可以自动统计容器中各元素出现的次数。

Counter 的本质就是一个特殊的 dict,它的 key 是包含的元素,value 是 key 的个数。如果通过 Counter 并不存在的 key 访问 value,将会输出 0(因为key 出现了 0 次)。

Counter 三个常用的方法:

1、elements():该方法返回该 Counter 所包含的全部元素组成的迭代器。

2、most_common([n]):该方法返回 Counter 中出现最多的 n 个元素。

3、subtract([iterable-or-mapping]):该方法计算 Counter 的减法,其实就是计算减去之后各元素出现的次数。




arr = ['a','a','b','b','c','c','a','c','d','f','g']
a_count=len([i for i in arr if i=='a'])
b_count=len([i for i in arr if i=='b'])
c_count=len([i for i in arr if i=='c'])
others_count=len(arr)-a_count-b_count-c_count
print("a出现的次数",a_count)
print("b出现的次数",b_count)
print("c出现的次数",c_count)
print("其他字母出现的次数",others_count)


扩展阅读:python求列表每个元素个数 ... python中如何统计数的个数 ... 用python计算各科成绩总分 ... python利用列表分析成绩 ... 输出列表元素个数python ... python matplotlib ... python统计红楼梦出现次数 ... python统计列表字母出现次数 ... python 统计列表元素个数 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网