作者ya790206 (残云夺月)
看板Python
标题Re: [问题] 关於 index 用法
时间Mon Jul 7 16:05:30 2008
※ 引述《plom (plom)》之铭言:
: 大家好, 请教各位:
: a=['abc','cdrf','abc','erd','abc']
: 则 a.index = 0
: 是否可有办法一次找出所有"abc" 的 index 位置?
: 例 a.index = [0,2,4]
看一下内建函数好像没有这个功能
自己写一个献丑一下
def indexs(data,sub):
i=0
result=[]
try:
while 1:
i=data.index(sub,i)
result.append(i)
i+=1
except ValueError,e:
pass
return result
a=['a','b','a']
print indexs(a,'a')
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.116.141.78