Python - 将多个 Pickle 对象加载到单个字典中
Python - Load multiple Pickle objects into a single dictionary
所以我的问题是...我有多个 Pickle 对象文件(它们是 Pickled 词典),我想加载它们,但实际上将每个词典合并成一个更大的词典。
例如
我有 pickle_file1 和 pickle_file2 都包含字典。我想将 pickle_file1 和 pickle_file2 的内容加载到 my_dict_final.
编辑
根据要求,这是我目前所拥有的:
for pkl_file in pkl_file_list:
pickle_in = open(pkl_file,'rb')
my_dict = pickle.load(pickle_in)
pickle_in.close()
本质上,它是有效的,但只是覆盖了 my_dict 的内容,而不是附加每个 pickle 对象。
在此先感谢您的帮助。
my_dict_final = {} # Create an empty dictionary
with open('pickle_file1', 'rb') as f:
my_dict_final.update(pickle.load(f)) # Update contents of file1 to the dictionary
with open('pickle_file2', 'rb') as f:
my_dict_final.update(pickle.load(f)) # Update contents of file2 to the dictionary
print my_dict_final
您可以使用dict.update
功能。
pickle_dict1 = pickle.load(picke_file1)
pickle_dict2 = pickle.load(picke_file2)
my_dict_final = pickle_dict1
my_dict_final.update(pickle_dict2)
@Nunchux、@Vikas Ojha 如果字典碰巧有公共键,不幸的是,update
方法将覆盖这些公共键的值。示例:
>>> dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
>>> dict2 = {'a': 1, 'b': 8, 'c': 5}
>>> All_dict = {}
>>> All_dict.update(dict1)
>>> All_dict.update(dict2)
>>> All_dict
{'a': 1, 'b': 8, 'c': 5, 'd': 4}
如果您想避免这种情况并继续增加常用键的计数,一种选择是使用以下策略。应用于您的示例,这是一个最小的工作示例:
import os
import pickle
from collections import Counter
dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
dict2 = {'a': 1, 'b': 8, 'c': 5}
# just creating two pickle files:
pickle_out = open("dict1.pickle", "wb")
pickle.dump(dict1, pickle_out)
pickle_out.close()
pickle_out = open("dict2.pickle", "wb")
pickle.dump(dict2, pickle_out)
pickle_out.close()
# Here comes:
pkl_file_list = ["dict1.pickle", "dict2.pickle"]
All_dict = Counter({})
for pkl_file in pkl_file_list:
if os.path.exists(pkl_file):
pickle_in = open(pkl_file, "rb")
dict_i = pickle.load(pickle_in)
All_dict = All_dict + Counter(dict_i)
print (dict(All_dict))
这会很高兴地给你:
{'a': 5, 'b': 11, 'd': 4, 'c': 5}
所以我的问题是...我有多个 Pickle 对象文件(它们是 Pickled 词典),我想加载它们,但实际上将每个词典合并成一个更大的词典。
例如
我有 pickle_file1 和 pickle_file2 都包含字典。我想将 pickle_file1 和 pickle_file2 的内容加载到 my_dict_final.
编辑 根据要求,这是我目前所拥有的:
for pkl_file in pkl_file_list:
pickle_in = open(pkl_file,'rb')
my_dict = pickle.load(pickle_in)
pickle_in.close()
本质上,它是有效的,但只是覆盖了 my_dict 的内容,而不是附加每个 pickle 对象。
在此先感谢您的帮助。
my_dict_final = {} # Create an empty dictionary
with open('pickle_file1', 'rb') as f:
my_dict_final.update(pickle.load(f)) # Update contents of file1 to the dictionary
with open('pickle_file2', 'rb') as f:
my_dict_final.update(pickle.load(f)) # Update contents of file2 to the dictionary
print my_dict_final
您可以使用dict.update
功能。
pickle_dict1 = pickle.load(picke_file1)
pickle_dict2 = pickle.load(picke_file2)
my_dict_final = pickle_dict1
my_dict_final.update(pickle_dict2)
@Nunchux、@Vikas Ojha 如果字典碰巧有公共键,不幸的是,update
方法将覆盖这些公共键的值。示例:
>>> dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
>>> dict2 = {'a': 1, 'b': 8, 'c': 5}
>>> All_dict = {}
>>> All_dict.update(dict1)
>>> All_dict.update(dict2)
>>> All_dict
{'a': 1, 'b': 8, 'c': 5, 'd': 4}
如果您想避免这种情况并继续增加常用键的计数,一种选择是使用以下策略。应用于您的示例,这是一个最小的工作示例:
import os
import pickle
from collections import Counter
dict1 = {'a': 4, 'b': 3, 'c': 0, 'd': 4}
dict2 = {'a': 1, 'b': 8, 'c': 5}
# just creating two pickle files:
pickle_out = open("dict1.pickle", "wb")
pickle.dump(dict1, pickle_out)
pickle_out.close()
pickle_out = open("dict2.pickle", "wb")
pickle.dump(dict2, pickle_out)
pickle_out.close()
# Here comes:
pkl_file_list = ["dict1.pickle", "dict2.pickle"]
All_dict = Counter({})
for pkl_file in pkl_file_list:
if os.path.exists(pkl_file):
pickle_in = open(pkl_file, "rb")
dict_i = pickle.load(pickle_in)
All_dict = All_dict + Counter(dict_i)
print (dict(All_dict))
这会很高兴地给你:
{'a': 5, 'b': 11, 'd': 4, 'c': 5}