脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现统计给定列表中指定数字出现次数的方法

Python实现统计给定列表中指定数字出现次数的方法

2021-01-30 00:07Together_CZ Python

这篇文章主要介绍了Python实现统计给定列表中指定数字出现次数的方法,涉及Python针对列表的简单遍历、计算相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现统计给定列表中指定数字出现次数的方法。分享给大家供大家参考,具体如下:

直接看实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:给定一个列表计数指定数字出现的所有次数
'''
def count_num_func(num_list,num):
  '''''
  计数指定数字
  '''
  split_list=[]
  for one in num_list:
    split_list+=list(str(one))
  print split_list.count(str(num))
if __name__ == '__main__':
  num_list=[1,12,34,56,112,1243,870,45,450,12,23,56]
  print "服务器之家测试结果:"
  count_num_func(num_list,1)
  count_num_func(num_list,0)

结果如下:

Python实现统计给定列表中指定数字出现次数的方法

希望本文所述对大家Python程序设计有所帮助。

原文链接:https://blog.csdn.net/together_cz/article/details/77493969

延伸 · 阅读

精彩推荐