Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find all index positions

1 view
Skip to first unread message

mick...@hotmail.com

unread,
May 11, 2006, 11:20:57 AM5/11/06
to
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

Tim Chase

unread,
May 11, 2006, 11:58:12 AM5/11/06
to mick...@hotmail.com, pytho...@python.org

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


alisonken1

unread,
May 11, 2006, 12:04:07 PM5/11/06
to

==========
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}

Peter Otten

unread,
May 11, 2006, 12:26:05 PM5/11/06
to
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

Scott David Daniels

unread,
May 11, 2006, 12:44:35 PM5/11/06
to
mick...@hotmail.com wrote:
> astring = 'abcd efgd 1234 fsdf gfds abcde 1234'
> <paraphrase> i want to find all positions of '1234' in astring.</paraphrase>

def positions(target, source):
'''Produce all positions of target in source'''
pos = -1
try:
while True:
pos = source.index(target, pos + 1)
yield pos
except ValueError:
pass

print list(positions('1234', 'abcd efgd 1234 fsdf gfds abcde 1234'))

prints:
[10, 31]

--Scott David Daniels
scott....@acm.org

Paul Rubin

unread,
May 11, 2006, 12:53:33 PM5/11/06
to

Most straightforwardly with re.findall -- see the docs.

Gary Herron

unread,
May 11, 2006, 1:05:38 PM5/11/06
to pytho...@python.org
mick...@hotmail.com wrote:

The regular expression module (called re) has a function (named
finditer) that gives you what you want here.

The finditer function will find all matches of a pattern in a string and
return an iterator for them. You can loop through the iterator and do
what you want with each occurrence.

>>> import re


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

>>> pattern = '1234'

Perhaps just find the starting point of each match:

>>> for match in re.finditer(pattern,astring):
... print match.start()
...
10
31

Or the span of each match:

>>> for match in re.finditer(pattern,astring):
... print match.span()
...
(10, 14)
(31, 35)

Or use list comprehension to build a list of starting positions:

>>> [match.start() for match in re.finditer(pattern,astring)]
[10, 31]

And so on ....

Of course, if you wish, the re module can work with vastly more complex
patterns than just a constant string like your '1234'.

Gary Herron


Gary Herron

unread,
May 11, 2006, 1:08:27 PM5/11/06
to pytho...@python.org
Paul Rubin wrote:

Not quite. The findall will list the matching strings, not their
positions. -- He'll get ['1234','1234']. The finditer function will
work for his requirements. See my other post to this thread.

vbgunz

unread,
May 11, 2006, 3:13:46 PM5/11/06
to
I thought this to be a great exercise so I went the extra length to
turn it into a function for my little but growing library. I hope you
enjoy :)


def indexer(string, target):
'''indexer(string, target) -> [list of target indexes]

enter in a string and a target and indexer will either return a
list of all targeted indexes if at least one target is found or
indexer will return None if the target is not found in sequence.

>>> indexer('a long long day is long', 'long')
[2, 7, 19]

>>> indexer('a long long day is long', 'day')
[12]

>>> indexer('a long long day is long', 'short')
None
'''

res = []

if string.count(target) >= 1:
res.append(string.find(target))

if string.count(target) >= 2:
for item in xrange(string.count(target) - 1):
res.append(string.find(target, res[-1] + 1))

return res


if __name__ == '__main__':
print indexer('a long long day is long', 'long') # -> [2, 7, 19]
print indexer('a long long day is long', 'day') # -> [12]
print indexer('a long long day is long', 'short') # -> None

alisonken1

unread,
May 11, 2006, 3:14:53 PM5/11/06
to

Scott David Daniels wrote:
> mick...@hotmail.com wrote:
<SNIP>

> print list(positions('1234', 'abcd efgd 1234 fsdf gfds abcde 1234'))
>
> prints:
> [10, 31]
>

Nicer than mine ;)
Shows I need to get a job where I use python more!

John Machin

unread,
May 11, 2006, 6:58:16 PM5/11/06
to vbgunz
On 12/05/2006 5:13 AM, vbgunz wrote:
> I thought this to be a great exercise so I went the extra length to
> turn it into a function for my little but growing library. I hope you
> enjoy :)
>

Oh, indeed ;-)

>
> def indexer(string, target):

Consider not shadowing the name of the string module.

> '''indexer(string, target) -> [list of target indexes]
>
> enter in a string and a target and indexer will either return a
> list of all targeted indexes if at least one target is found or
> indexer will return None if the target is not found in sequence.

Consider returning [] if the target is not found in the sequence. That
would enable callers to "do nothing gracefully":

for posn in indexer(...
# do something

and it doesn't require a gross rewrite ... merely dedent the last line.

>
> >>> indexer('a long long day is long', 'long')
> [2, 7, 19]
>
> >>> indexer('a long long day is long', 'day')
> [12]
>
> >>> indexer('a long long day is long', 'short')
> None
> '''
>
> res = []
>

Consider evaluating string.count(target) *ONCE*.

> if string.count(target) >= 1:
> res.append(string.find(target))
>
> if string.count(target) >= 2:
> for item in xrange(string.count(target) - 1):
> res.append(string.find(target, res[-1] + 1))
>
> return res
>
>
> if __name__ == '__main__':
> print indexer('a long long day is long', 'long') # -> [2, 7, 19]
> print indexer('a long long day is long', 'day') # -> [12]
> print indexer('a long long day is long', 'short') # -> None
>

When called with the args('abababababababa', 'aba'), it returns [0, 2,
4, 6]. If you *intend* to allow for overlaps, it should return [0, 2, 4,
6, 8, 10, 12]; otherwise it should return [0, 4, 8, 12].

Consider doing something straightforward and understandable, like the
following (tested):

def findallstr(text, target, overlapping=0):
result = []
startpos = 0
if overlapping:
jump = 1
else:
jump = max(1, len(target))
while 1:
newpos = text.find(target, startpos)
if newpos == -1:
return result
result.append(newpos)
startpos = newpos + jump

HTH,
John

vbgunz

unread,
May 12, 2006, 11:45:43 AM5/12/06
to
Hello John,

Thank you very much for your pointers! I decided to redo it and try to
implement your suggestion. I think I did a fair job and because of your
suggestion have a better iterator. Thank you!

def indexer(string, substring, overlap=1):
'''indexer(string, substring, [overlap=1]) -> int

indexer takes a string and searches it to return all substring
indexes. by default indexer is set to overlap all occurrences.
to get the index to whole words only, set the overlap argument
to the length of the substring. The only pitfall to indexer is
it will return the substring whether it stansalone or not.

>>> list(indexer('ababababa', 'aba'))
[0, 2, 4, 6]

>>> list(indexer('ababababa', 'aba', len('aba')))
[0, 4]

>>> list(indexer('ababababa', 'xxx'))
[]

>>> list(indexer('show chow', 'how'))
[1, 6]
'''

index = string.find(substring)
if index != -1:
yield index

while index != -1:
index = string.find(substring, index + overlap)
if index == -1: continue
yield index

if __name__ == '__main__':
print list(indexer('ababababa', 'aba')) # -> [0, 2, 4, 6]
print list(indexer('ababababa', 'aba', len('aba'))) # -> [0, 4]
print list(indexer('ababababa', 'xxx')) # -> []
print list(indexer('show chow', 'how')) # -> [1, 6]

vbgunz

unread,
May 12, 2006, 11:55:37 AM5/12/06
to
I forgot to explain my reason for over shadowing the 'string' built-in
within my iterator. To me, it doesn't matter because the string
identifier is temporary within the function and dies when the function
dies. Also, I personally don't use the string function and prefer
''.join('hi'), etc. Also, at least for me just starting out in Python,
I find 'string' to be as readable as possible :)

what do you think about that?

John Machin

unread,
May 12, 2006, 5:59:15 PM5/12/06
to vbgunz
On 13/05/2006 1:45 AM, vbgunz wrote:
> Hello John,
>
> Thank you very much for your pointers! I decided to redo it and try to
> implement your suggestion. I think I did a fair job and because of your
> suggestion have a better iterator. Thank you!
>
> def indexer(string, substring, overlap=1):
> '''indexer(string, substring, [overlap=1]) -> int
>
> indexer takes a string and searches it to return all substring
> indexes. by default indexer is set to overlap all occurrences.
> to get the index to whole words only, set the overlap argument
> to the length of the substring.

(1) Computing the length should be done inside the function, if
necessary, which (2) avoids the possibility of passing in the wrong
length. (3) "whole words only" does *NOT* mean the same as "substrings
don't overlap".

> The only pitfall to indexer is
> it will return the substring whether it stansalone or not.
>
> >>> list(indexer('ababababa', 'aba'))
> [0, 2, 4, 6]
>
> >>> list(indexer('ababababa', 'aba', len('aba')))
> [0, 4]
>
> >>> list(indexer('ababababa', 'xxx'))
> []
>
> >>> list(indexer('show chow', 'how'))
> [1, 6]
> '''
>
> index = string.find(substring)
> if index != -1:
> yield index
>
> while index != -1:
> index = string.find(substring, index + overlap)
> if index == -1: continue
> yield index

Quite apart from the fact that you are now using both 'string' *AND*
'index' outside their usual meaning, this is hard to follow. (1) You
*CAN* avoid doing the 'find' twice without losing readibility and
elegance. (2) continue?? Somebody hits you if you use the 'return'
statement or the 'break' statement?

Sigh. I'll try once more. Here is the function I wrote, with the minimal
changes required to make it an iterator, plus changing from 0/1 to
False/True:

def findallstr(text, target, overlapping=False):


startpos = 0
if overlapping:
jump = 1
else:
jump = max(1, len(target))

while True:


newpos = text.find(target, startpos)
if newpos == -1:
return

yield newpos


startpos = newpos + jump

>

> if __name__ == '__main__':
> print list(indexer('ababababa', 'aba')) # -> [0, 2, 4, 6]
> print list(indexer('ababababa', 'aba', len('aba'))) # -> [0, 4]
> print list(indexer('ababababa', 'xxx')) # -> []
> print list(indexer('show chow', 'how')) # -> [1, 6]
>

Get yourself a self-checking testing mechanism, and a more rigorous set
of tests. Ultimately you will want to look at unittest or pytest, but
for a small library of functions, you can whip up your own very quickly.
Here is what I whipped up yesterday:

def indexer2(string, target):
res = []


if string.count(target) >= 1:
res.append(string.find(target))
if string.count(target) >= 2:
for item in xrange(string.count(target) - 1):
res.append(string.find(target, res[-1] + 1))

return res # dedent fixed

if __name__ == '__main__':
tests = [
('a long long day is long', 'long', [2, 7, 19], [2, 7, 19]),
('a long long day is long', 'day', [12], [12]),
('a long long day is long', 'short', [], []),
('abababababababa', 'aba', [0, 4, 8, 12], [0, 2, 4, 6, 8, 10, 12]),
('qwerty', '', range(7), range(7)),
('', 'qwerty', [], []),
]
for test in tests:
text, target = test[:2]
results = test[2:]
for olap in range(2):
result = findallstr(text, target, olap)
print (
'FAS', text, target, olap,
result, results[olap], result == results[olap],
)
for test in tests:
text, target = test[:2]
results = test[2:]
result = indexer2(text, target)
print (
'INDXR2', text, target,
result, result == results[0], result == results[1],
)

Make sure your keyboard interrupt is not disabled before you run the
2nd-last test :-)

HTH,
John

John Machin

unread,
May 12, 2006, 6:40:50 PM5/12/06
to vbgunz

1. 'string' is not a function, it is a module. I'm glad you don't use
the non-existent 'string function'.

2.

>>> ''.join('hi')
'hi'

You prefer to do that instead of what?

Look at the docs on the .join() method carefully, and consider the
following:
>>> '-'.join('hello')
'h-e-l-l-o'
>>>

3. Back to the shadowing thing:

'string' is admittedly not a good example to pull you up on, as that
module is little used these days. However it is the thin end of the
wedge, and your adding 'index' in round 2 just drove the wedge in a
little further. Once you start using words like 'file' and 'list' as
variable names, your code will be almost as *UN*readable as possible.
It's not just the shadowing possibility, it's the confusion in the
reader's mind between the usual/normal Python meaning of a word and the
meaning that you have attached to it. As you are just starting out in
Python, now is the time to acquire good habits.

Cheers,
John

0 new messages