2nd iteration of a character

P

Philippe Rousselot

Hi,

How do I find the second iteration of a character in a string using python

this gives me the first one
f1 = string.find(line,sub)

this the last one
f2 = string.rfind(line,sub)

but for the nth one ?

Thanks in advance

Philippe

PS : any good book to recommend ?
 
J

Jeff Epler

You probably want to write this in terms of find with a start argument
specified. This minimally-tested function seems to do the job.

def findn(haystack, needle, n, start=0):
while 1:
idx = haystack.find(needle, start)
if idx == -1 or n==1: break
n -= 1
start = idx+1
return idx
-1

Jeff
 
B

Bengt Richter

Hi,

How do I find the second iteration of a character in a string using python

this gives me the first one
f1 = string.find(line,sub)

this the last one
f2 = string.rfind(line,sub)

but for the nth one ?

Thanks in advance

Philippe

PS : any good book to recommend ?
Online tutorials first ;-)

... i = -len(ss)
... for j in xrange(n):
... i += len(ss)
... i = s.find(ss,i)
... if i<0: return i
... return i<0 and -1 or i
...
>>> find_nth(s,'x', 1) 2
>>> find_nth(s,'x', 2) 5
>>> find_nth(s,'x', 3) 9
>>> [find_nth(s,'x', i) for i in xrange(5)] [-1, 2, 5, 9, 16]
>>> [find_nth(s,'x7', i) for i in xrange(5)] [-1, 16, -1, -1, -1]
>>> [find_nth(s,'01', i) for i in xrange(5)] [-1, 0, 10, -1, -1]
>>> [find_nth(s,'34', i) for i in xrange(5)] [-1, 3, 13, -1, -1]
>>> [find_nth(s,'xx', i) for i in xrange(5)]
[-1, -1, -1, -1, -1]

BTW, the above is not the fastest possible code, just a quick first prototype,
and not tested beyond what you see above ;-)


Regards,
Bengt Richter
 
E

Emile van Sebille

How do I find the second iteration of a character in a string using python
this gives me the first one
f1 = string.find(line,sub)

this the last one
f2 = string.rfind(line,sub)

but for the nth one ?

You've already got some good answers, so how about (-:

len(ss.join(s.split(ss)[:n]))
....

Emile van Sebille
(e-mail address removed)
 
A

Anthony McDonald

Philippe Rousselot said:
Hi,

How do I find the second iteration of a character in a string using python

this gives me the first one
f1 = string.find(line,sub)

this the last one
f2 = string.rfind(line,sub)

but for the nth one ?

Thanks in advance

Philippe

PS : any good book to recommend ?

Most of the answers you've been given take the form (haystack, needle,
iteration), so I thought it'd be nice to write a little iterator.

class Ifind:
def __init__(self, haystack, needle):
self.haystack = haystack
self.needle = needle
self.pos = 0
def __iter__(self):
return self
def next(self):
found = self.haystack[self.pos:].find(self.needle)
if found == -1:
raise StopIteration
found = self.pos + found
self.pos = found + 1
return found
.... print a
....
2
12
22.... print a
....
0
10
20.... print a
....
Anthony McDonald
 
B

Bengt Richter

Philippe Rousselot said:
Hi,

How do I find the second iteration of a character in a string using python

this gives me the first one
f1 = string.find(line,sub)

this the last one
f2 = string.rfind(line,sub)

but for the nth one ?

Thanks in advance

Philippe

PS : any good book to recommend ?

Most of the answers you've been given take the form (haystack, needle,
iteration), so I thought it'd be nice to write a little iterator.

class Ifind:
def __init__(self, haystack, needle):
self.haystack = haystack
self.needle = needle
self.pos = 0
def __iter__(self):
return self
def next(self):
found = self.haystack[self.pos:].find(self.needle)
if found == -1:
raise StopIteration
found = self.pos + found
self.pos = found + 1
return found
... print a
...
2
12
22

Carrying on with that theme ...

You could use the re module, e.g., (observing that a constant string with no magic characters
is a simple regular expression, though you could of course make up more complex things with re).
...
2
12
22
... print a
...
0
10
20
...
0
10
20
... print a
...

Or if you do want to roll your own find-based iterable, the generator version seems easier to me:
... pos=0; skip = len(needle)
... while 1:
... pos = haystack.find(needle, pos)
... if pos<0: break
... yield pos
... pos += skip
... ...
2
12
22 ...
0
10
20
Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top