问题描述
嗨
说我有这样的字符串
astring =''abcd efgd 1234 fsdf gfds abcde 1234''
如果我想要的话找到哪个位置是1234,我怎样才能实现这一目标......?我想b / b
想要使用index()但它只给我第一次出现。我想
知道两者的位置1234
谢谢
嗯,我不确定它的效率如何,但以下是
似乎是为我做的:
[10,31]
HTH,
-tkc
==========
def getAllIndex(aString = None,aSub = None):
t = dict()
c = 0
ndx = 0
而True:
试试:
ndx = aString.index(aSub,ndx)
t [c] = ndx
ndx + = 1
c + = 1
除了ValueError:
break
返回t
===========
这将返回找到的字典;即,
{0:5,1:15}
{0:5,1:15}
从0开始的连续整数作为键?看起来你想要一个清单
而不是一个字典。
彼得
hi
say i have string like this
astring = ''abcd efgd 1234 fsdf gfds abcde 1234''
if i want to find which postion is 1234, how can i achieve this...? i
want to use index() but it only give me the first occurence. I want to
know the positions of both "1234"
thanks
Well, I''m not sure how efficient it is, but the following
seemed to do it for me:
[10, 31]
HTH,
-tkc
==========
def getAllIndex(aString=None, aSub=None):
t=dict()
c=0
ndx=0
while True:
try:
ndx=aString.index(aSub, ndx)
t[c]=ndx
ndx += 1
c += 1
except ValueError:
break
return t
===========
This will return a dictionary of what was found; i.e.,
{0: 5, 1: 15}
{0: 5, 1: 15}
Consecutive integers starting at 0 as keys? Looks like you want a list
instead of a dict.
Peter
这篇关于找到所有指数头寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!