Linux 拨号vps windows公众号手机端

python如何统计列表中元素个数

lewis 9年前 (2016-11-08) 阅读数 7 #程序编程
文章标签 python

要统计列表中元素的个数,可以使用Python内置的len()函数。示例如下:

my_list = [1, 2, 3, 4, 5]
count = len(my_list)
print(count)  # 输出:5

另外,也可以使用列表的count()方法来统计特定元素在列表中出现的次数。示例如下:

my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
count_2 = my_list.count(2)
count_4 = my_list.count(4)
print(count_2)  # 输出:2
print(count_4)  # 输出:4
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门