简介
我有一个字典,格式如下:
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”更改为“list of dict”,如下所示:
dict_dict = {'S0': {'list0': [list of int], 'list1': [list of int]}, ...}
在访问列表时会产生更易读的代码:
dict_dict['S0']['list0'] and dict_dict['S0']['list1']
泡菜问题
然而,当我腌制并将dict_dict保存到文件时,结果表明额外的dict键的实际比例与“S#”条目的数量成比例。似乎pickle没有“聪明地”存储dict,因为它分别存储每个dict键。
现在,我们意识到这毕竟是pickle应该如何工作,因为每个'S#'可能有不同的键组开始。 pickle没有办法事先知道我们的dict_dict实际上只是一个有规律重复字段的表。
问题
我的问题是,是否有dict_list
的替代方案,其中可以通过字符串键访问整数列表(如dict_dict
中所述),但没有上述腌制惩罚?
更新:基于评论的实验
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")