python中count函數(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ù)。具體介紹請看本文。
1、count函數(shù)
統(tǒng)計(jì)列表ls中value元素出現(xiàn)的次數(shù)
2、語法
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ù)為字符串長度即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ù)知識點(diǎn)淺析的文章就介紹到這了,更多相關(guān)python中count函數(shù)是什么意思內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 使用Maven 搭建 Spring MVC 本地部署Tomcat的詳細(xì)教程2. IntelliJ IDEA配置Tomcat服務(wù)器的方法3. idea刪除項(xiàng)目的操作方法4. IntelliJ IDEA導(dǎo)入jar包的方法5. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法6. IntelliJ IDEA恢復(fù)刪除文件的方法7. idea重置默認(rèn)配置的方法步驟8. idea導(dǎo)入maven項(xiàng)目的方法9. Docker 部署 Prometheus的安裝詳細(xì)教程10. IntelliJ IDEA調(diào)整字體大小的方法

網(wǎng)公網(wǎng)安備