1.常规方法 初始化一个字典,遍历列表或字符串,如果遍历的值已经存在于字典中,则字典值直接加1,否则,令字典键为当前遍历的值,字典值为1, 代码如下: >>> dic = {} >>> li = ['a','a','a','b','b','b','c','c','d'] >>> for i in li: if i in dic.keys(): dic[i] += 1 else: dic[i] = 1 >>> print(dic) {'…
Python dictionary 字典 常用法 d = {} d.has_key(key_in) # if has the key of key_in d.keys() # keys list d.values() # values list d.get(key_in,[defualt]) # it will return 'NoneType' or [default] if with the secon…