简体   繁体   English

Python - 将多个 Pickle 对象加载到单个字典中

[英]Python - Load multiple Pickle objects into a single dictionary

So my problem is this... I have multiple Pickle object files (which are Pickled Dictionaries) and I want to load them all, but essentially merge each dictionary into a single larger dictionary.所以我的问题是......我有多个 Pickle 对象文件(它们是 Pickled Dictionaries),我想加载它们,但本质上将每个字典合并到一个更大的字典中。

Eg例如

I have pickle_file1 and pickle_file2 both contain dictionaries.我有 pickle_file1 和 pickle_file2 都包含字典。 I would like the contents of pickle_file1 and pickle_file2 loaded into my_dict_final.我想将pickle_file1 和pickle_file2 的内容加载到my_dict_final 中。

EDIT As per request here is what i have so far:编辑根据请求,这是我到目前为止所拥有的:

for pkl_file in pkl_file_list:
    pickle_in = open(pkl_file,'rb')
    my_dict = pickle.load(pickle_in)
    pickle_in.close()

In essence, it works, but just overwrites the contents of my_dict rather than append each pickle object.本质上,它有效,但只是覆盖 my_dict 的内容而不是附加每个泡菜对象。

Thanks in advance for the help.在此先感谢您的帮助。

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

You can use the dict.update function.您可以使用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)

Python Standard Library Docs Python 标准库文档

@Nunchux, @Vikas Ojha If the dictionaries happen to have common keys, the update method will, unfortunately, overwrite the values for those common keys. @Nunchux、@Vikas Ojha 如果字典碰巧有公共键,不幸的是, update方法将覆盖这些公共键的值。 Example:例子:

>>> 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}

If you'd like to avoid this and keep adding the counts of common keys, one option is to use the following strategy.如果您想避免这种情况并继续添加公共键的计数,一种选择是使用以下策略。 Applied to your example, here is a minimal working example:应用于您的示例,这是一个最小的工作示例:

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))

This will happily give you:这将很高兴为您提供:

{'a': 5, 'b': 11, 'd': 4, 'c': 5}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM