Counting how many chars equal to a given char are in the beginning of a string

S

Stormbringer

Hi,

Given a string s and a char c, is there a short & elegant way to
determine how many consecutive occurences of c are in the beginning of
s ?

For example if s = ">>>>message1" and c = ">" then the number I am
looking for is 4 (the string begins with 4 '>').

Thanks,
Andrei
 
J

Jeff Epler

Here are several ways:

def count_initial(s, c):
return len(s) - len(s.lstrip(c))

def count_initial(s, c):
r = "^" + re.escape(c) + "*"
m = re.match(r, s)
return len(m.group(0))

# enumerate in 2.3
def count_initial(s, c):
for i, j in enumerate(s):
if j != c: break
return i

# itertools in 2.3
def count_initial(s, c):
return len(list(itertools.takewhile(lambda x: x==c, s)))

Jeff
 
S

Skip Montanaro

Andrei> Given a string s and a char c, is there a short & elegant way to
Andrei> determine how many consecutive occurences of c are in the
Andrei> beginning of s ?

Andrei> For example if s = ">>>>message1" and c = ">" then the number I
Andrei> am looking for is 4 (the string begins with 4 '>').

How about:

def howmanyatstart(s, pfx):
return (len(s) - len(s.lstrip(pfx)))/len(pfx)

? This works for prefixes which are longer than a single character:
3

Skip
 
V

vincent wehren

| Hi,
|
| Given a string s and a char c, is there a short & elegant way to
| determine how many consecutive occurences of c are in the beginning of
| s ?
|
| For example if s = ">>>>message1" and c = ">" then the number I am
| looking for is 4 (the string begins with 4 '>').

How about:

def getc(s, c):
"""
Get the number of consecutive occurrences of given char c
at the beginning of given string s.
"""
cnt = 0
while 1:
try:
if s[cnt] == c:
cnt += 1
else:
return cnt
except IndexError:
return cnt

c = '>'
print getc(">>>>Message", c) #prints 4
print getc(" >>>>Message", c) #prints 0
print getc (">>>", c) #prints 3

Maybe not the most elegant or shortest, but it works...

HTH,

Vincent Wehren


|
| Thanks,
| Andrei
 
V

vincent wehren

| | | Hi,
| |
| | Given a string s and a char c, is there a short & elegant way to
| | determine how many consecutive occurences of c are in the beginning of
| | s ?
| |
| | For example if s = ">>>>message1" and c = ">" then the number I am
| | looking for is 4 (the string begins with 4 '>').
|
| How about:
|
| def getc(s, c):
| """
| Get the number of consecutive occurrences of given char c
| at the beginning of given string s.
| """
| cnt = 0
| while 1:
| try:
| if s[cnt] == c:
| cnt += 1
| else:
| return cnt
| except IndexError:
| return cnt
|
| c = '>'
| print getc(">>>>Message", c) #prints 4
| print getc(" >>>>Message", c) #prints 0
| print getc (">>>", c) #prints 3
|
| Maybe not the most elegant or shortest, but it works...

Looking at Jeff's proposed solutions, I would like to replace
the "Maybe" part in the above sentence with "Definitively"...

Isn't Python great?

Vincent




Vincent Wehren


|
| HTH,
|
| Vincent Wehren
|
|
| |
| | Thanks,
| | Andrei
|
|
 
J

Jeff Epler

How about:

def howmanyatstart(s, pfx):
return (len(s) - len(s.lstrip(pfx)))/len(pfx)

? This works for prefixes which are longer than a single character:

3

strip() doesn't work that way:'xxx'
 
S

Skip Montanaro

Jeff> strip() doesn't work that way: Jeff> 'xxx'

Then it looks like a bug in one or the other to me.

Skip
 
S

Skip Montanaro

Jeff> strip() doesn't work that way: Jeff> 'xxx'

Skip> Then it looks like a bug in one or the other to me.

I retract my statement. It's a bug in my code. help("".lstrip) shows why:

lstrip(...)
S.lstrip([chars]) -> string or unicode

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

Note the second sentence.

Sorry for the flub.

Skip
 
O

Oren Tirosh

Hi,

Given a string s and a char c, is there a short & elegant way to
determine how many consecutive occurences of c are in the beginning of
s ?

For example if s = ">>>>message1" and c = ">" then the number I am
looking for is 4 (the string begins with 4 '>').

re.match('>*', s).end()

Oren
 
B

Bengt Richter

Hi,

Given a string s and a char c, is there a short & elegant way to
determine how many consecutive occurences of c are in the beginning of
s ?

For example if s = ">>>>message1" and c = ">" then the number I am
looking for is 4 (the string begins with 4 '>').
Not tested beyond this example ;-)
4

Regards,
Bengt Richter
 
B

Bengt Richter

Not tested beyond this example ;-)

4
I wouldn't have posted this if the Re: replies had been in the same thread
tree in my reader. Guess I need to upgrade ;-/

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top