
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
1、count()
用于统计字符串中某个字符出现的次数。可选参数为文字串检索的开始和结束位置。返回子字符串在字符串中出现的次数。
count()方法语法
list.count(obj)
参数
obj,列表中统计的对象。
实例
defcount_each_char_1(string):
res={}
foriinstring:
ifinotinres:
res[i]=1
else:
res[i]+=1
returnres
print(count_each_char_1('aenabsascd'))
2、List count()
用于统计某些要素在列表中出现的次数。返回元素在列表中出现的次数。
aList=[123,'Google','Runoob','Taobao',123];
print("123元素的个数:",aList.count(123))
print("Taobao元素的个数",aList.count('Taobao'))