当前位置:   article > 正文

python中count函数的用法_python中countinue的用法

python中countinue的用法

count():统计在字符串/列表/元组中某个字符出现的次数。
count()统计字符串某字符出现的次数:

string:
		str.count(sub, start= 0,end=len(string))或 str.count("char") 
		str —— 为要统计的字符(可以是单字符,也可以是多字符)。
		star —— 为索引字符串的起始位置,默认参数为0。
		end —— 为索引字符串的结束位置,默认参数为字符串长度即len(str)。
		
		示例:
			def test_a():
				str = "i like python,i am learning python"
				print(str.count("i")) #star 和end 为默认参数
				print(str.count("i",2)) # star值为2,end值为默认参数
				print(str.count("i",2,5)) #star值为2,end值为5
				print(str.count("am"))  #多字符统计
		
		运行结果:
		4
		3
		1
		1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

count()统计列表中某元素出现的次数:

def test_a():
	list=["i","love","python",10,"love",45,20,"python",10,"love"]
	list_num_0 = list.count(10)
	list_num_1 = list.count("love")
	print("love:{},10:{}".format(list_num_1, list_num_0))

运行结果:
	love:3,10:2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

count()统计元组中某元素出现的次数:

def test_a():
	tuple=("i","love","python",10,"love",45,20,"python",10,"love")
	tuple_num_0 = tuple.count(10)
	tuple_num_1 = tuple.count("love")
	print("love:{},10:{}".format(tuple_num_1, tuple_num_0))

运行结果:
	love:3,10:2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/535167
推荐阅读
相关标签
  

闽ICP备14008679号