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
==========
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}
> ==========
> 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
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
Most straightforwardly with re.findall -- see the docs.
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
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.
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
Nicer than mine ;)
Shows I need to get a job where I use python more!
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
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]
what do you think about that?
(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
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