新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:怎么利用python統(tǒng)計字典中元素出現(xiàn)的次數(shù)?
其實,應該很多小伙伴都不明白學習這個內(nèi)容有什么意義主要是,因為我們呢很少在程序應用里,去使用這些功能,事實真的如此嘛?大家要清楚我們學習編程,里面的程序邏輯思維和蜘蛛網(wǎng)一樣密密麻麻, 所以不要小看任何一個內(nèi)容哦~具體內(nèi)容如下:

示例:列表中有很多字典,需要統(tǒng)計字典中相同的鍵對應的值的和。
解決代碼:
利用 defaultdict 設置默認值的方法 defaultdict(int)
In [6]: d = defaultdict(int)
In [7]: objs = [{'F29958SVDK6': 12}, {'F29958SVDK6': 12}, {'F29958SVDK6': 12}, {'F29958SVDK6': 12}, {'F29958SVDK6': 12}]
In [8]: for obj in objs:
...: for key,value in obj.items():
...: d[key] += value
...:
In [9]: d
Out[9]: defaultdict(int, {'F29958SVDK6': 60})好啦,大家如果遇到類似的問題,就可以看看上述小編提交的方式方法,進行測試啦,任何一個相似的內(nèi)容,都可以按照上述內(nèi)容去代入解決哦~
文章題目:創(chuàng)新互聯(lián)Python教程:怎么利用python統(tǒng)計字典中元素出現(xiàn)的次數(shù)?
文章起源:http://m.5511xx.com/article/cdecdcs.html


咨詢
建站咨詢
