Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
行中相同字符的数目-python_Python_String - Fatal编程技术网

行中相同字符的数目-python

行中相同字符的数目-python,python,string,Python,String,我有一个字符(如“a”),我需要检查一个字符串(如“aaaabcd”)中“a”在一行中出现的次数(在本例中,处理在“b”处停止,返回值为4) 我有这样的想法: def count_char(str_, ch_): count = 0 for c in str_: if c == ch_: count += 1 else: return count 所以我在想。。。有没有更好/更通灵/更简单的方法来实现这一点 重新匹配函数将开始查找字符串的开头 m

我有一个字符(如“a”),我需要检查一个字符串(如“aaaabcd”)中“a”在一行中出现的次数(在本例中,处理在“b”处停止,返回值为4)

我有这样的想法:

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

所以我在想。。。有没有更好/更通灵/更简单的方法来实现这一点

重新匹配
函数将开始查找字符串的开头

m = re.match(r'[%s]+' % ch_, str_)
return m.end() if m else 0
如果希望字符串任何部分中的字符数最大:

max(len(x) for x in re.findall(r'[%s]+' % ch_, str_))
一个选项使用


您可以从
itertools
模块中借用:

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
之后

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

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

如果你真的在意,你可以使用
sum(1代表uu in.)
而不是
len(list(…)
来避免列表的具体化,但我发现我在晚年时对类似的事情不太在意。^)

如果只关心字符串的开头,可以使用
lstrip
并比较长度:

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

也许不是最有效的方法,但很可能是最简单的。

因此,如果字符串是
baaabcd
,它应该返回
0
?这个问题似乎与谷歌搜索中发现的问题相同:“python在字符串中计算重复字符”。@TimPietzcker-是,如果它是
aaabcdaaa
它应该返回
3
@CharlesBurns谢谢,也许我也可以使用它,但我不认为它真的是重复的,因为我只需要一行中发生的次数,并停在不同的字符上,不管以后是否在字符串againThanks中找到计数的字符,re.match工作得非常完美,我应该更仔细地检查re模块文档。您必须转义正则表达式中特殊的字符。
ch
不是内置的,所以不确定为什么要给它一个尾随符underscore@jamylak正确的。我只是使用OP的变量名。@jamylak习惯命名函数参数,但我发现根据PEP8,它是用来避免与keywords@NZT是的,这就是我困惑的原因。在这件事上,处理并没有停在bcase@jamylak我不确定你的意思。我不确定,但是
x.lstrip
必须创建一个新字符串,无论如何,它可能仍然使用旧的内存+1@lqc谢谢,也许是最简单的解决办法
>>> from itertools import takewhile
>>> sum(1 for c in takewhile('a'.__eq__, 'aaaabcd'))
4
>>> x = "aaaabcd"
>>> len(x) - len(x.lstrip("a"))
4