gpt4 book ai didi

python - 如何避免 pickle 字典与 pickle 列表的惩罚

转载 作者:行者123 更新时间:2023-11-30 23:36:25 25 4
gpt4 key购买 nike

简介

我有一本具有以下格式的字典:

dict_list = {'S0':[[list of int],[list of int]], 'S1':[[list of int],[list of int]], ...}

通过 S0 访问整数列表

dict_list['S0'][0] and dict_list['S0'][1]

为了提高代码可读性,我将“list of list”更改为“dict of list”,如下所示:

dict_dict = {'S0': {'list0': [list of int], 'list1': [list of int]}, ...}

这会在访问列表时产生更具可读性的代码:

dict_dict['S0']['list0'] and dict_dict['S0']['list1']

pickle 问题

但是,当我 pickle 并将 dict_dict 保存到文件时,事实证明,拥有额外字典键的惩罚实际上与“S#”条目的数量成比例。看来 pickle 并没有“智能”地存储字典,因为它单独存储每个字典键。

现在,我们意识到这毕竟是 pickle 应该如何工作的,因为每个“S#”一开始可能有不同的键集。 pickle 无法事先知道我们的 dict_dict 实际上只是一个具有定期重复字段的表。

问题

我的问题是,是否有 dict_list 的替代方案,其中可以通过字符串键访问整数列表(如 dict_dict 中),但没有所述的 pickle 惩罚上面?

更新:根据给出的评论进行实验

3,100 bytes - dict_list['S0'][0] (list.bin)
3,314 bytes - dict_dict['S0']['list0'] (dict.bin)
3,922 bytes - dict_class['S0'].list0 (class.bin)
5,855 bytes - dict_namedtuple['S0'].list0 (namedtuple.bin)
<小时/>
s_list = ['S0','S1','S2','S3','S4','S5','S6','S7','S8','S9','S10','S11','S12','S13','S14','S15','S0a','S1a','S2a','S3a','S4a','S5a','S6a','S7a','S8a','S9a','S10a','S11a','S12a','S13a','S14a','S15a','AA0','AA1','AA2','AA3','AA4','AA5','AA6','AA7','AA8','AA9','AA10','AA11','AA12','AA13','AA14','AA15','AA0a','AA1a','AA2a','AA3a','AA4a','AA5a','AA6a','AA7a','AA8a','AA9a','AA10a','AA11a','AA12a','AA13a','AA14a','AA15a','BB0','BB1','BB2','BB3','BB4','BB5','BB6','BB7','BB8','BB9','BB10','BB11','BB12','BB13','BB14','BB15','BB0a','BB1a','BB2a','BB3a','BB4a','BB5a','BB6a','BB7a','BB8a','BB9a','BB10a','BB11a','BB12a','BB13a','BB14a','BB15a']
num_of_s_entries = 32
list_length = 5

def pickle_n_save(dict_var, filename):
outfile = open(filename, "wb")
pickle.dump(dict_var, outfile)
outfile.close()

# ------------------------------------------------------------dict_list['S0'][0]
dict_list = {}
for s in s_list[0:num_of_s_entries]:
dict_list[s] = [[],[]]
for pts in range(0,list_length):
dict_list[s][0].append(randrange(1,100))
dict_list[s][1].append(randrange(1,100)*1000)

pickle_n_save(dict_list, "list.bin")

# -----------------------------------------------------dict_dict['S0']['list0']
dict_dict = {}
for s in dict_list.keys():
dict_dict[s] = {}
dict_dict[s]['list0'] = dict_list[s][0]
dict_dict[s]['list1'] = dict_list[s][1]

pickle_n_save(dict_dict, "dict.bin")

# -------------------------------------------------------dict_class['S0'].list0
class S:
def __init__(self, list0, list1):
self.list0 = list0
self.list1 = list1

dict_class = {}
for s in dict_list.keys():
dict_class[s] = S(dict_list[s][0],dict_list[s][1])

pickle_n_save(dict_class, "class.bin")

# ---------------------------------------------------dict_namedtuple['S0'].list0
S_namedtuple = namedtuple('S_namedtuple', ['list0','list1'])
dict_namedtuple = {}
for s in dict_list.keys():
dict_namedtuple[s] = S_namedtuple(dict_list[s][0],dict_list[s][1])

pickle_n_save(dict_namedtuple, "namedtuple.bin")

最佳答案

也许您想要 namedtuple

关于python - 如何避免 pickle 字典与 pickle 列表的惩罚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16493852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com