在工作中我們經(jīng)常要統(tǒng)計數(shù)字甥郑,字符渗钉,文件名出現(xiàn)的次數(shù)郭厌,常用的方法是字典携茂,其實還有個Counter可以使用
1.字典方法
L = ["a","b","c","d","e","f","g","j","o","a","c","d","e","b","e","g","a","a","c","d"]
d = {}
for i in L:
if i in d:
d[i] += 1
else:
d[i] = 1
print(d)
2.使用collections里面的Counter
from collections import Counter
L = ["a","b","c","d","e","f","g","j","o","a","c","d","e","b","e","g","a","a","c","d"]
print(dict(Counter(L)))
當然還有很多可以統(tǒng)計比如某個目錄下的所有目錄里有的文件你踩,有多少個名稱是一樣,也可以統(tǒng)計出來,像統(tǒng)計單詞個數(shù)之類的都可以使用带膜,不過使用前要先生成list