找到所有指数头寸 [英] find all index positions

查看:94
本文介绍了找到所有指数头寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



说我有这样的字符串

astring =''abcd efgd 1234 fsdf gfds abcde 1234''

如果我想要的话找到哪个位置是1234,我怎样才能实现这一目标......?我想b / b
想要使用index()但它只给我第一次出现。我想

知道两者的位置1234

谢谢

解决方案

>说我有这样的字符串

astring =''abcd efgd 1234 fsdf gfds abcde 1234''
如果我想找到哪个位置是1234,我怎么能实现这个...?我想使用index(),但它只给我第一次出现。我想知道
两者的位置1234




嗯,我不确定它的效率如何,但以下是

似乎是为我做的:

a =''abcd efgd 1234 fsdf gfds abcde 1234''
thing ='''1234''
offsets = [i for i in range(len(a))if
a.startswith(thing,i)] print offsets



[10,31]

HTH,


-tkc




mi *** **** @ hotmail.com 写道:


说我有这样的字符串
astring =''abcd efgd 1234 fsdf gfds abcde 1234''
如果我想找到哪个位置是1234,我怎么能实现这个......?我想使用index()但它只给我第一次出现。我想知道两者的位置1234
谢谢




==========

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

===========


这将返回找到的字典;即,

getAllIndex(''abcd 1234 efgh 1234 ijkl'',''1234'')



{0:5,1:15}


alisonken1写道:

==========
def getAllIndex(aString = None,aSub = None):
t = dict()
c = 0
ndx = 0
同时为真:
尝试:
ndx = aString.index(aSub,ndx)
t [c] = ndx
ndx + = 1
c + = 1
除了ValueError:
break
return t
===========

这将返回找到的字典;即,

getAllIndex(''abcd 1234 efgh 1234 ijkl'',''1234'')


{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

解决方案

> 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"



Well, I''m not sure how efficient it is, but the following
seemed to do it for me:

a = ''abcd efgd 1234 fsdf gfds abcde 1234''
thing = ''1234''
offsets = [i for i in range(len(a)) if a.startswith(thing, i)] print offsets


[10, 31]
HTH,

-tkc




mi*******@hotmail.com wrote:

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



==========
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.,

getAllIndex(''abcd 1234 efgh 1234 ijkl'', ''1234'')


{0: 5, 1: 15}


alisonken1 wrote:

==========
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.,

getAllIndex(''abcd 1234 efgh 1234 ijkl'', ''1234'')


{0: 5, 1: 15}



Consecutive integers starting at 0 as keys? Looks like you want a list
instead of a dict.

Peter


这篇关于找到所有指数头寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆