1

介绍

我有一本格式如下的字典:

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']

泡菜问题

但是,当我将 dict_dict 腌制并保存到文件时,事实证明,拥有额外的 dict 键的代价实际上与“S#”条目的数量成比例。似乎泡菜没有“智能”地存储字典,因为它分别存储了每个字典键。

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

问题

我的问题是,是否有替代方法dict_list可以通过字符串键(如 in 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")
4

1 回答 1

3

也许你想要一个namedtuple

于 2013-05-11T05:11:21.413 回答