Python count()方法和Python List count()方法
2016-09-06 11:25
351 查看
本文转载自http://www.runoob.com/python/att-string-count.html
http://www.runoob.com/python/att-list-count.html
侵删
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
count()方法语法:
sub -- 搜索的子字符串
start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
该方法返回子字符串在字符串中出现的次数。
以下实例展示了count()方法的实例:
以上实例输出结果如下:
count() 方法用于统计某个元素在列表中出现的次数。
count()方法语法:
obj -- 列表中统计的对象。
返回元素在列表中出现的次数。
以下实例展示了 count()函数的使用方法:
以上实例输出结果如下:
http://www.runoob.com/python/att-list-count.html
侵删
描述
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
语法
count()方法语法:str.count(sub, start= 0,end=len(string))
参数
sub -- 搜索的子字符串start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
返回值
该方法返回子字符串在字符串中出现的次数。
实例
以下实例展示了count()方法的实例:#!/usr/bin/python str = "this is string example....wow!!!"; sub = "i"; print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40) sub = "wow"; print "str.count(sub) : ", str.count(sub)
以上实例输出结果如下:
str.count(sub, 4, 40) : 2 str.count(sub, 4, 40) : 1
描述
count() 方法用于统计某个元素在列表中出现的次数。
语法
count()方法语法:list.count(obj)
参数
obj -- 列表中统计的对象。
返回值
返回元素在列表中出现的次数。
实例
以下实例展示了 count()函数的使用方法:#!/usr/bin/python aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for 123 : ", aList.count(123); print "Count for zara : ", aList.count('zara');
以上实例输出结果如下:
Count for 123 : 2 Count for zara : 1
相关文章推荐
- Python中List.count()方法的使用教程
- Python中List.count()方法的使用教程
- 【Python】Python3 List count()方法
- python返回数组(list)长度的方法
- python两个 list 获取交集,并集,差集的方法
- Python 元组 count() 方法
- python 整型int 布尔值boolean 列表list 简介及常用方法大全
- 基于python list对象中嵌套元组使用sort时的排序方法
- python中List的sort方法(或者sorted内建函数)的用法
- Python处理CSV与List的转换方法
- python基础===两个list合并成一个dict的方法
- python里面的set、list、tuple区别和使用方法
- python学习系列---list,tuple,dict,set内置方法
- 【Python】Python List list()方法
- python分割列表(list)的方法示例
- Python index()方法和Python List index()方法
- python之列表(list)的使用方法介绍
- 【Python】Python3 List insert()方法
- Python os.listdir() 方法
- python list 基本方法