Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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_Python 3.x - Fatal编程技术网

Python 对字符串列表中的特定单个字母进行计数

Python 对字符串列表中的特定单个字母进行计数,python,python-3.x,Python,Python 3.x,注意:不是一封重复的信。我想知道每封信的数量,你贴的那封重复的信给出了每封信的总数 我试图从存储在列表中的所有字符串中计算单个字母 def countElement(a): g = {} for i in a: if i in g: g[i] +=1 else: g[i] =1 return g list : ['a a b b c c', 'a c b c', 'b c c a b']

注意:不是一封重复的信。我想知道每封信的数量,你贴的那封重复的信给出了每封信的总数

我试图从存储在列表中的所有字符串中计算单个字母

def countElement(a):
    g = {}
    for i in a:
        if i in g: 
            g[i] +=1
        else: 
            g[i] =1
    return g

list : ['a a b b c c', 'a c b c', 'b c c a b']



  for i in range(1000000):
        b = countElement(list)
    print(b)
目前,这会产生以下结果:

{'a a b b c c': 1, 'a c b c': 1, 'b c c a b': 1}
但我真正想要达到的结果是:

a = 4
b = 5
c = 6
我可以使用另一个函数来计算列表中字符串中的单个字母吗?

当然可以!使用:


这个问题与您用来标记为重复的问题略有不同。
from collections import Counter


lst = ['a a b b c c', 'a c b c', 'b c c a b']

counter = Counter()
for word in lst:
    counter.update(word)

print(counter)
# Counter({' ': 12, 'c': 6, 'b': 5, 'a': 4})