2

I have a character (eg. "a") and I need to check a string (eg. "aaaabcd") for the number of occurances of "a" in a row (processing stops at "b" in this case and returned value is 4).

I have something like this:

def count_char(str_, ch_):
  count = 0
  for c in str_:
    if c == ch_:
      count += 1
    else:
      return count

So I was thinking... Is there a better/more pythonic/simplier way to do this?

NZT
  • 51
  • 1
  • 4
  • 4
    So if the string is `baaaabcd`, it should return `0`? – Tim Pietzcker Jun 02 '13 at 18:12
  • This question appears to be a duplicate of http://stackoverflow.com/questions/991350/counting-repeated-characters-in-a-string-in-python which was found with the Google search: "python count repeated characters in string". – Charles Burns Jun 02 '13 at 18:19
  • @TimPietzcker - Yes, and if it is `aaabcdaaa` it should return `3` – NZT Jun 02 '13 at 18:24
  • 1
    @CharlesBurns Thanks for that, maybe I could use that too, but I do not think it is really duplicate as I need only number of occurances in a row and stop at different character no matter if the counted character is later found in the string again – NZT Jun 02 '13 at 18:26

5 Answers5

4

The re.match function will start looking in the beginning of the string

m = re.match(r'[%s]+' % ch_, str_)
return m.end() if m else 0

If you want the biggest number of chars in any part of the string:

max(len(x) for x in re.findall(r'[%s]+' % ch_, str_))
jamylak
  • 128,818
  • 30
  • 231
  • 230
JBernardo
  • 32,262
  • 10
  • 90
  • 115
4

One option using itertools.takewhile,

>>> from itertools import takewhile
>>> str_ = 'aaaabcd'
>>> ch_ = 'a'
>>> sum(1 for _ in takewhile(lambda x: x == ch_, str_))
4
Jared
  • 25,627
  • 7
  • 56
  • 61
2

If you only care about the beginning of the string, you could use lstrip and compare lengths:

>>> x = "aaaabcd"
>>> len(x) - len(x.lstrip("a"))
4

Maybe not the most efficient way, but most likely the simplest.

lqc
  • 7,434
  • 1
  • 25
  • 25
0

You could borrow from the itertools module:

from itertools import takewhile, groupby

def startcount1(s, c):
    group = takewhile(lambda x: x == c, s)
    return len(list(group))

def startcount2(s, c):
    key, group = next(groupby(s))
    return len(list(group)) if key == c else 0

After which

tests = ['aaaabcd', 'baaaabcd', 'abacadae', 'aaabcdaaa']
for test in tests:
    print test,
    for f in count_char, startcount1, startcount2:
        print f(test, 'a'),
    print

will produce

aaaabcd 4 4 4
baaaabcd 0 0 0
abacadae 1 1 1
aaabcdaaa 3 3 3

If you really cared you could use sum(1 for _ in ..) instead of len(list(..)) to avoid materializing the list, but I find I care less about things like that in my old age. :^)

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
DSM
  • 342,061
  • 65
  • 592
  • 494
0
>>> from itertools import takewhile
>>> sum(1 for c in takewhile('a'.__eq__, 'aaaabcd'))
4
jamylak
  • 128,818
  • 30
  • 231
  • 230