ihujie

python中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
str.index(2,7,10)
ValueError: 2 is not in list

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2021-11-22
  • 2021-10-23
  • 2021-03-31
  • 2022-12-23
  • 2021-07-12
猜你喜欢
  • 2021-12-29
  • 2022-01-12
  • 2021-07-02
  • 2021-11-23
  • 2021-06-26
  • 2022-12-23
相关资源
相似解决方案