python中count函數(shù)知識點淺析
python中,count函數(shù)的作用是進(jìn)行python中的數(shù)量計算。count函數(shù)用于統(tǒng)計字符串、列表或元祖中某個字符出現(xiàn)的次數(shù),是一個很好用的統(tǒng)計函數(shù)。具體介紹請看本文。
1、count函數(shù)
統(tǒng)計列表ls中value元素出現(xiàn)的次數(shù)
2、語法
str.count('char', start,end)
或
str.count('char') -> int 返回整數(shù)
3、參數(shù)
str —— 為要統(tǒng)計的字符(可以是單字符,也可以是多字符)。
star —— 為索引字符串的起始位置,默認(rèn)參數(shù)為0。
end —— 為索引字符串的結(jié)束位置,默認(rèn)參數(shù)為字符串長度即len(str)。
4、返回值
返回統(tǒng)計參數(shù)出現(xiàn)的次數(shù)
5、實例
list 中 某元素 的次數(shù)
list = [10, 20, 30, ’Hello’, 10, 20]print 'list.count(’Hello’) : ', list.count(’Hello’)print 'list.count(10) : ', list.count(10)
輸出
list.count(’Hello’) : 1list.count(10) : 2
實例擴(kuò)展:
實例(Python 2.0+)
#!/usr/bin/pythonstr = '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)
以上實例輸出結(jié)果如下:
str.count(sub, 4, 40) : 2str.count(sub) : 1
到此這篇關(guān)于python中count函數(shù)知識點淺析的文章就介紹到這了,更多相關(guān)python中count函數(shù)是什么意思內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
