关于python:计算字符串中字符的出现次数

Count the number occurrences of a character in a string

计算字符串中字符出现次数的最简单方法是什么?

例如,计算'a'出现在'Mary had a little lamb'中的次数。


str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

1
2
3
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4


您可以使用count():

1
2
>>> 'Mary had a little lamb'.count('a')
4


正如其他答案所说,使用string方法count()可能是最简单的,但是如果您经常这样做,请签出collections.counter:

1
2
3
4
from collections import Counter
my_str ="Mary had a little lamb"
counter = Counter(my_str)
print counter['a']


可能是正则表达式?

1
2
3
import re
my_string ="Mary had a little lamb"
len(re.findall("a", my_string))


1
myString.count('a');

更多信息在这里


1
"aabc".count("a")


str.count(a)是计算字符串中单个字符的最佳解决方案。但是,如果您需要计数更多的字符,您必须读取整个字符串,读取的次数与您想要计数的字符相同。

更好的方法是:

1
2
3
4
5
6
7
from collections import defaultdict

text = 'Mary had a little lamb'
chars = defaultdict(int)

for char in text:
    chars[char] += 1

因此,您将得到一个dict,它返回字符串中每个字母的出现次数,如果不存在,则返回0

1
2
3
4
>>>chars['a']
4
>>>chars['x']
0

对于不区分大小写的计数器,可以通过子类化defaultdict来重写mutator和accessor方法(基类的方法是只读的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class CICounter(defaultdict):
    def __getitem__(self, k):
        return super().__getitem__(k.lower())

    def __setitem__(self, k, v):
        super().__setitem__(k.lower(), v)


chars = CICounter(int)

for char in text:
    chars[char] += 1

>>>chars['a']
4
>>>chars['M']
2
>>>chars['x']
0


如果您希望不区分大小写(当然还有regex的所有功能),正则表达式非常有用。

1
2
3
4
5
6
7
8
9
10
my_string ="Mary had a little lamb"
# simplest solution, using count, is case-sensitive
my_string.count("m")   # yields 1
import re
# case-sensitive with regex
len(re.findall("m", my_string))
# three ways to get case insensitivity - all yield 2
len(re.findall("(?i)m", my_string))
len(re.findall("m|M", my_string))
len(re.findall(re.compile("m",re.IGNORECASE), my_string))

请注意,regex版本的运行时间约为10倍,只有当我的字符串非常长,或者代码在一个深循环中时,这可能是一个问题。


这个简单而直接的功能可能有助于:

1
2
3
4
5
6
7
8
def check_freq(str):
    freq = {}
    for c in str:
       freq[c] = str.count(c)
    return freq

check_freq("abbabcbdbabdbdbabababcbcbab")
{'a': 7, 'b': 14, 'c': 3, 'd': 3}


1
2
3
4
a = 'have a nice day'
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
    print key, a.count(key)

count绝对是计算字符串中字符出现次数的最简洁和最有效的方法,但我尝试使用lambda提出一个解决方案,类似于:

1
2
sentence = 'Mary had a little lamb'
sum(map(lambda x : 1 if 'a' in x else 0, sentence))

这将导致:

1
4

另外,还有一个好处是,如果句子是包含与上面相同字符的子字符串列表,那么由于使用了in,这也会给出正确的结果。看一看:

1
2
sentence = ['M', 'ar', 'y', 'had', 'a', 'little', 'l', 'am', 'b']
sum(map(lambda x : 1 if 'a' in x else 0, sentence))

这也会导致:

1
4

但当然,只有在这种特殊情况下检查单个字符(如'a'的出现)时,这才有效。


1
2
3
4
5
6
7
8
9
str ="count a character occurance"

List = list(str)
print (List)
Uniq = set(List)
print (Uniq)

for key in Uniq:
    print (key, str.count(key))

"不使用count在字符串中查找所需字符"方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import re

def count(s, ch):

   pass

def main():

   s = raw_input ("Enter strings what you like, for example, 'welcome':")  

   ch = raw_input ("Enter you want count characters, but best result to find one character:" )

   print ( len (re.findall ( ch, s ) ) )

main()


不使用Counter()count和regex获取所有字符计数的另一种方法

1
2
3
4
5
6
7
8
counts_dict = {}
for c in list(sentence):
  if c not in counts_dict:
    counts_dict[c] = 0
  counts_dict[c] += 1

for key, value in counts_dict.items():
    print(key, value)

不超过此imho-您可以添加上下方法

1
2
def count_letter_in_str(string,letter):
    return string.count(letter)

使用计数:

1
2
string ="count the number of counts in string to count from."
x = string.count("count")

x=3。


1
2
3
4
5
6
7
8
9
10
11
12
spam = 'have a nice day'
var = 'd'


def count(spam, var):
    found = 0
    for key in spam:
        if key == var:
            found += 1
    return found
count(spam, var)
print 'count %s is: %s ' %(var, count(spam, var))

这将给您一个字符串中每个字符的出现次数。O/P也采用字符串格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def count_char(string1):
string2=""
lst=[]
lst1=[]
for i in string1:
    count=0
    if i not in lst:
        for j in string1:
            if i==j:
                count+=1
        lst1.append(i)
        lst1.append(count)
    lst.append(i)

string2=''.join(str(x) for x in lst1)
return string2

print count_char("aabbacddaabbdsrchhdsdg")