-1
想查找彙總統計我有一本字典:爲具有多個值的Python字典
a_dic = {'file1':["a","b","c"],
'file2':["b","c","d"],
'file3':["c","d","e"]}
我想編寫一個函數能夠返回字典/數據框中找到像按鍵的發生:
occurrence = {'a':1, 'b':2, 'c':3, 'd':2,'e':1}
想查找彙總統計我有一本字典:爲具有多個值的Python字典
a_dic = {'file1':["a","b","c"],
'file2':["b","c","d"],
'file3':["c","d","e"]}
我想編寫一個函數能夠返回字典/數據框中找到像按鍵的發生:
occurrence = {'a':1, 'b':2, 'c':3, 'd':2,'e':1}
隨着collections.Counter
對象和itertools.chain.from_iterable
功能:
import collections, itertools
a_dic = {'file1':["a","b","c"], 'file2':["b","c","d"], 'file3':["c","d","e"]}
result = dict(collections.Counter(itertools.chain.from_iterable(a_dic.values())))
print(result)
輸出:
{'c': 3, 'e': 1, 'b': 2, 'd': 2, 'a': 1}
from collections import Counter
flat_list = [item for sublist in (a_dic.values()) for item in sublist]
print(Counter(flat_list))
輸出
Counter({'c': 3, 'b': 2, 'd': 2, 'a': 1, 'e': 1})
是您的代碼不工作? – nutmeg64