python中count函數(shù)知識(shí)點(diǎn)淺析
python中,count函數(shù)的作用是進(jìn)行python中的數(shù)量計(jì)算。count函數(shù)用于統(tǒng)計(jì)字符串、列表或元祖中某個(gè)字符出現(xiàn)的次數(shù),是一個(gè)很好用的統(tǒng)計(jì)函數(shù)。具體介紹請(qǐng)看本文。
1、count函數(shù)
統(tǒng)計(jì)列表ls中value元素出現(xiàn)的次數(shù)
2、語(yǔ)法
str.count('char', start,end)
或
str.count('char') -> int 返回整數(shù)
3、參數(shù)
str —— 為要統(tǒng)計(jì)的字符(可以是單字符,也可以是多字符)。
star —— 為索引字符串的起始位置,默認(rèn)參數(shù)為0。
end —— 為索引字符串的結(jié)束位置,默認(rèn)參數(shù)為字符串長(zhǎng)度即len(str)。
4、返回值
返回統(tǒng)計(jì)參數(shù)出現(xiàn)的次數(shù)
5、實(shí)例
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
實(shí)例擴(kuò)展:
實(shí)例(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)
以上實(shí)例輸出結(jié)果如下:
str.count(sub, 4, 40) : 2str.count(sub) : 1
到此這篇關(guān)于python中count函數(shù)知識(shí)點(diǎn)淺析的文章就介紹到這了,更多相關(guān)python中count函數(shù)是什么意思內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. css進(jìn)階學(xué)習(xí) 選擇符2. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案3. XML入門(mén)的常見(jiàn)問(wèn)題(一)4. 解析原生JS getComputedStyle5. 阿里前端開(kāi)發(fā)中的規(guī)范要求6. UDDI FAQs7. html小技巧之td,div標(biāo)簽里內(nèi)容不換行8. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序9. 刪除docker里建立容器的操作方法10. Echarts通過(guò)dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖
