简体   繁体   中英

How to count the number of characters at the start of a string?

How can I count the number of characters at the start/end of a string in Python?

For example, if the string is

'ffffhuffh'

How would I count the number of f s at the start of the string? The above string with a f should output 4.

str.count is not useful to me as a character could be in the middle of the string.

A short and simple way will be to use the str.lstrip method, and count the difference of length.

s = 'ffffhuffh'
print(len(s)-len(s.lstrip('f')))
# output: 4

str.lstrip([chars]) :

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed.

Try this, using itertools.takewhile() :

import itertools as it

s = 'ffffhuffh'
sum(1 for _ in it.takewhile(lambda c: c == 'f', s))
=> 4

Similarly, for counting the characters at the end:

s = 'huffhffff'
sum(1 for _ in it.takewhile(lambda c: c == 'f', reversed(s)))
=> 4

You may use regular expression with re.match to find the occurrence of any character at the start of the string as:

>>> import re
>>> my_str = 'ffffhuffh'
>>> my_char = 'f'

>>> len(re.match('{}*'.format(my_char), my_str).group())
4

Building on Oscar Lopez's answer, I want to handle the case you mention of the end of the string: use reversed()

import itertools as it

my_string = 'ffffhuffh'

len(list(it.takewhile(lambda c: c == my_string[-1], reversed(my_string))))
=> 1

You can create a function and iterate through your string and return the count of the desired char in the input string's beginning or end like this example:

# start = True: Count the chars in the beginning of the string
# start = False: Count the chars in the end of the string
def count_char(string= '', char='', start=True):
    count = 0
    if not start:
        string = string[::-1]

    for k in string:
        if k is char:
            count += 1
        else:
            break
    return count

a = 'ffffhuffh'
print(count_char(a, 'f'))
b = a[::-1]
print(count_char(b, 'f', start=False))

Output:

4
4

You may also use itertools.groupby to find the count of the occurrence of the first element at the start of the string as:

from itertools import groupby

def get_first_char_count(my_str):
    return len([list(j) for _, j in groupby(my_str)][0])

Sample run:

>>> get_first_char_count('ffffhuffh')
4
>>> get_first_char_count('aywsnsb')
1

re.sub选择带重复的第一个字母((^(\\ w)\\ 2 *)),len计数频率。

len(re.sub(r'((^\w)\2*).*',r'\1',my_string))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM