1、字符串中查找值和对应的下标

a = "123yui78y8y67tuy"
print re.findall("y",a)  #查找出在a包含的字符串中所有的y值
print [i for i, x in enumerate(a) if x == 'y']  #查找出a包含的y值所对应的下标

——————————————————————结果————————————————————————

['y', 'y', 'y', 'y']
[3, 8, 10, 15]

 

2、列表中查找值和对应的下标

l = [23,45,4,2,4,2,4]   
result = []    #给定空列表,将结果值写入此列表中
for i in range(l.count(4)):   
    if i==0:
        pos=l.index(4)
    else:
        pos=l.index(4,pos+1)
    result.append(pos)
print result

________________________结果____________________________
[2, 4, 6]

 

相关文章:

  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
  • 2021-06-26
  • 2021-11-15
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
相关资源
相似解决方案