Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Counting how many chars equal to a given char are in the beginning of a string

Reply
Thread Tools

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

 
 
Stormbringer
Guest
Posts: n/a
 
      12-22-2003
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
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      12-22-2003
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

 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      12-22-2003

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:

>>> howmanyatstart(">>>>message1", '>')

4
>>> howmanyatstart("bobbobbob>>>>message1", 'bob')

3

Skip

 
Reply With Quote
 
vincent wehren
Guest
Posts: n/a
 
      12-22-2003
"Stormbringer" <> schrieb im Newsbeitrag
news: om...
| 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


 
Reply With Quote
 
vincent wehren
Guest
Posts: n/a
 
      12-22-2003
"vincent wehren" <> schrieb im Newsbeitrag
news:bs7nrc$ubp$...
| "Stormbringer" <> schrieb im Newsbeitrag
| news: om...
| | 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
|
|


 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      12-22-2003
On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
> 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:
>
> >>> howmanyatstart(">>>>message1", '>')

> 4
> >>> howmanyatstart("bobbobbob>>>>message1", 'bob')

> 3


strip() doesn't work that way:
>>> "bbbbbbbxxx".strip("bob")

'xxx'

 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      12-22-2003
>>>>> "Jeff" == Jeff Epler <> writes:

Jeff> On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
>> 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:
>>
>> >>> howmanyatstart(">>>>message1", '>')

>> 4
>> >>> howmanyatstart("bobbobbob>>>>message1", 'bob')

>> 3


Jeff> strip() doesn't work that way:
>>>> "bbbbbbbxxx".strip("bob")

Jeff> 'xxx'

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

Skip

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      12-22-2003
On Mon, Dec 22, 2003 at 04:06:44PM -0600, Skip Montanaro wrote:
> Then it looks like a bug in one or the other to me.


Add 'assert len(c) == 1' at the top of mine, then.

Jeff

 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      12-22-2003
>>>>> "Jeff" == Jeff Epler <> writes:

Jeff> On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
>> How about:
>>
>> def howmanyatstart(s, pfx):
>> return (len(s) - len(s.lstrip(pfx)))/len(pfx)
>>
>> ?


Jeff> strip() doesn't work that way:
>>>> "bbbbbbbxxx".strip("bob")

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

 
Reply With Quote
 
Oren Tirosh
Guest
Posts: n/a
 
      12-23-2003
On Mon, Dec 22, 2003 at 12:04:21PM -0800, Stormbringer wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments