如何从python中的字符串创建字典

use*_*404 1 python string dictionary

my_str = 'wednesday'
Run Code Online (Sandbox Code Playgroud)

输出应该是

d = {'w':1,'e':2,'d':2,'n':1,'s':1,'y':1,'a':1}
Run Code Online (Sandbox Code Playgroud)

有没有直接的内置函数?

adr*_*tam 7

>>> import collections
>>> s = 'wednesday'
>>> collections.Counter(s)
Counter({'e': 2, 'd': 2, 'w': 1, 'n': 1, 's': 1, 'a': 1, 'y': 1})
Run Code Online (Sandbox Code Playgroud)