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