我正在尝试创建一个for循环,将值添加到已建立的字典中的键。但是,我总是得到最后一个值,而不是所有的值。我做错了什么?
我现在的字典是这样的:
growth_dict = dict.fromkeys(conc[1:9], '')
growth_dict = {'100 ug/ml': '', '12.5 ug/ml': '', '50 ug/ml': '',
'0 ug/ml': '', '6.25 ug/ml': '', '25 ug/ml': '', '3.125 ug/ml': '',
'1.5625 ug/ml': ''}
cols_list = numpy.loadtxt(fn, skiprows=1, usecols=range(1,9), unpack=True)
numer = (0.301)*960 #numerator
for i in cols_list:
N = i[-1]
No = i[0]
denom = (math.log(N/No)) #denominator
g = numer/denom当我运行该程序并键入"growth_dict“时,它返回我的字典,其中只有最后一个值作为键:
growth_dict = {'100 ug/ml': 131.78785283808514, '12.5 ug/ml': 131.78785283808514,
'50 ug/ml': 131.78785283808514, '0 ug/ml': 131.78785283808514,
'6.25 ug/ml': 131.78785283808514, '25 ug/ml': 131.78785283808514,
'3.125 ug/ml': 131.78785283808514, '1.5625 ug/ml': 131.78785283808514}发布于 2012-06-09 07:26:29
每次执行此操作时,都会覆盖conc[j]字典条目的值:
growth_dict[conc[j]] = g如果您想要将每个连续的g附加到字典条目,请尝试如下所示:
for j in conc:
# The first time each key is tested, an empty list will be created
if not instanceof(growth_dict[conc[j]], list):
growth_dict[conc[j]] = []
growth_dict[conc[j]].append(g)发布于 2012-06-09 07:29:36
您还可以通过执行以下操作来节省大量加载数据的工作
cols_list = numpy.loadtxt(fn, skiprows=1, usecols=range(1,9), unpack=True)https://stackoverflow.com/questions/10957083
复制相似问题