python中list的count和index用法舉例


>>> str = [1,2,3,4,5]                          #定義一個列表
>>> str *= 3                                   #列表*3
>>> str
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
>>> str.count(1)                               #1在str中出現的次數
3
>>> str.index(2)                               #2在str中第一次出現的位置
1
>>> str.index(2,7,12)                          #從index in(7,12)中查找2第一次出現的位置
11
>>> str.index(2,7,10)                          #從index in(7,10)中查找2第一次出現的位置,沒找到就會報錯

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    str.index(2,7,10)
ValueError: 2 is not in list

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM