如何将腌制对象加载到空变量列表中?
how to load pickled objects into a list of empty variables?
我有一个变量名列表,以及一个腌制对象的名称列表:
pickle_var_names = object1, object2 # both empty, which throws an error
pickle_names = ['object1', 'object2']
我希望将pickle_names
中调用的泡菜加载到pickle_var_names
中,分别为:
for pickle_var_name,pickle_name in zip(pickle_var_names,pickle_names):
with open(pickle_name,'r') as pickle_fp:
pickle_var_name = cPickle.load(pickle_fp)
如何将变量列表初始化为 None
以允许稍后分配它们?
列表是按位置而非名称索引的,因此您可以这样做:
picklevars = []
picklenames = ['object1', 'object2']
for picklename in picklenames:
with open(pickle_name, 'r') as pickle_fp:
picklevars.append(cPickle.load(pickle_fp))
或者,如果您能够使用字典而不是列表:
picklevars = {}
picklenames = ['object1', 'object2']
for picklename in picklenames:
with open(pickle_name, 'r') as pickle_fp:
picklevars[picklename] = cPickle.load(pickle_fp)
我有一个变量名列表,以及一个腌制对象的名称列表:
pickle_var_names = object1, object2 # both empty, which throws an error
pickle_names = ['object1', 'object2']
我希望将pickle_names
中调用的泡菜加载到pickle_var_names
中,分别为:
for pickle_var_name,pickle_name in zip(pickle_var_names,pickle_names):
with open(pickle_name,'r') as pickle_fp:
pickle_var_name = cPickle.load(pickle_fp)
如何将变量列表初始化为 None
以允许稍后分配它们?
列表是按位置而非名称索引的,因此您可以这样做:
picklevars = []
picklenames = ['object1', 'object2']
for picklename in picklenames:
with open(pickle_name, 'r') as pickle_fp:
picklevars.append(cPickle.load(pickle_fp))
或者,如果您能够使用字典而不是列表:
picklevars = {}
picklenames = ['object1', 'object2']
for picklename in picklenames:
with open(pickle_name, 'r') as pickle_fp:
picklevars[picklename] = cPickle.load(pickle_fp)