元素计数字典

man*_*ich 2 python dictionary

我想创建一个包含的字典{x,list.count[x]}.但是,在尝试执行我的代码时,我一直遇到错误:

apple = ['variant', 'variant', 'variant2.0', 'variant 3.0']

pear = {}

for x in apple:
    pear.update({x,apple.count(x)})
Run Code Online (Sandbox Code Playgroud)

错误是cannot convert dictionary update sequence element#0 to a sequence.任何建议?

wei*_*dev 5

您的字典定义中存在语法错误.尝试

pear.update({x : apple.count(x)})
Run Code Online (Sandbox Code Playgroud)