Python 如何从给定的列表输入中填充字典,其中值是字母,键是给定字母的引用

Python 如何从给定的列表输入中填充字典,其中值是字母,键是给定字母的引用,python,list,dictionary,Python,List,Dictionary,所以。我试图编写一个程序,用户在其中输入字母列表,即 l=['a','a','a','a','b','b','r',] 生成一个字典,其中键是每个字母,值是字母在列表中出现的次数,即: dictionary1={a:4, b:2, r:1, z:0} 这是我到目前为止试过的 def list1(l): dictionary1 = {} for x in l: dictionary1[x]:l.count(x) return dictionary1

所以。我试图编写一个程序,用户在其中输入字母列表,即

l=['a','a','a','a','b','b','r',]
生成一个字典,其中键是每个字母,值是字母在列表中出现的次数,即:

dictionary1={a:4, b:2, r:1, z:0}
这是我到目前为止试过的

def list1(l):
  
  dictionary1 = {}
  
  
  for x in l:
    dictionary1[x]:l.count(x)
  
  return dictionary1

但是输出是一个空字典。我应该如何进行?提前感谢

python中已经包含了最简单的功能。例如,这是标准解决方案:

from collections import Counter

l=['a','a','a','a','b','b','r',]
dictionary1 = Counter(l)

欢迎来到SO!
z
从哪里来?否则,看起来您有一个小的输入错误
dictionary1[x]:l.count(x)
应该是
dictionary1[x]=l.count(x)
从集合导入计数器列表1