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

Discussion in 'Python' started by Stormbringer, Dec 22, 2003.

  1. Stormbringer

    Stormbringer Guest

    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
     
    Stormbringer, Dec 22, 2003
    #1
    1. Advertising

  2. Stormbringer

    Jeff Epler Guest

    Re: Counting how many chars equal to a given char are in thebeginning of a string

    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
     
    Jeff Epler, Dec 22, 2003
    #2
    1. Advertising

  3. Re: Counting how many chars equal to a given char are in thebeginning of a string

    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
     
    Skip Montanaro, Dec 22, 2003
    #3
  4. "Stormbringer" <> schrieb im Newsbeitrag
    news:...
    | 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
     
    vincent wehren, Dec 22, 2003
    #4
  5. "vincent wehren" <> schrieb im Newsbeitrag
    news:bs7nrc$ubp$1.nb.home.nl...
    | "Stormbringer" <> schrieb im Newsbeitrag
    | news:...
    | | 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
    |
    |
     
    vincent wehren, Dec 22, 2003
    #5
  6. Stormbringer

    Jeff Epler Guest

    Re: Counting how many chars equal to a given char are in thebeginning of a string

    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'
     
    Jeff Epler, Dec 22, 2003
    #6
  7. Re: Counting how many chars equal to a given char are in thebeginning of a string

    >>>>> "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
     
    Skip Montanaro, Dec 22, 2003
    #7
  8. Stormbringer

    Jeff Epler Guest

    Re: Counting how many chars equal to a given char are in thebeginning of a string

    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
     
    Jeff Epler, Dec 22, 2003
    #8
  9. Re: Counting how many chars equal to a given char are in thebeginning of a string

    >>>>> "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
     
    Skip Montanaro, Dec 22, 2003
    #9
  10. Stormbringer

    Oren Tirosh Guest

    Re: Counting how many chars equal to a given char are in thebeginning of a string

    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
     
    Oren Tirosh, Dec 23, 2003
    #10
  11. On 22 Dec 2003 12:04:21 -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 '>').
    >

    Not tested beyond this example ;-)

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

    ...
    >>> s = ">>>>message1"
    >>> countleading('>', s)

    4

    Regards,
    Bengt Richter
     
    Bengt Richter, Dec 23, 2003
    #11
  12. On 23 Dec 2003 12:57:47 GMT, (Bengt Richter) wrote:

    >On 22 Dec 2003 12:04:21 -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 '>').
    >>

    >Not tested beyond this example ;-)
    >
    > >>> def countleading(c, s): return len(s)-len(s.lstrip(c))

    > ...
    > >>> s = ">>>>message1"
    > >>> countleading('>', s)

    > 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
     
    Bengt Richter, Dec 23, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,127
    Ian Collins
    May 9, 2006
  2. Hongyu
    Replies:
    9
    Views:
    948
    James Kanze
    Aug 8, 2008
  3. Lars Schouw
    Replies:
    1
    Views:
    386
    Sousuke
    Mar 26, 2010
  4. Andreas Leitgeb

    create a string of <n> equal chars <c>

    Andreas Leitgeb, Jul 13, 2010, in forum: Java
    Replies:
    32
    Views:
    1,338
    Kevin McMurtrie
    Jul 17, 2010
  5. Jesse B.
    Replies:
    9
    Views:
    249
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page