猿问
字典该怎么初始化?
像这个dict如何初始化呢
def his(s): d = dict() for c in s: d[c] += d.get(c,1) return d
狐的传说
浏览 670
回答 2
2回答
当年话下
from collections import defaultdict d = defaultdict(int) for c in s: d[c] += 1 Python 中的defaultdict和Counter可以对应 C++ 中的 multidict ( 楼主你的代码就是一个典型的多路字典的需求 )
0
0
0
慕妹3242003
from collections import Counter def his(s): return dict(Counter(s))
0
0
0
随时随地看视频
慕课网APP
相关分类
Python
我要回答