我试图从一个标题列表中生成dict,它将数据列“关联”到同一个实验。例如,我想转向:headers = ["A_1","A_2","A_3","B_1","B_2","B_3"]
进入
^{pr2}$
我的代码如下:cols = {}
headers = ["A_1","A_2","A_3","B_1","B_2","B_3"]
col_number = 0
for header in headers:
run_number = header[-1:]
cols[ run_number ] = {}
if "A_" in header:
cols[ run_number ][ 'A' ] = col_number
if "B_" in header:
cols[ run_number ][ 'B' ] = col_number
col_number += 1
print cols
这只输出最后的“B”列:{'1': {'B':3}, '2':{'B':5}... }
但是,如果我在命令shell上尝试一个简单的实验,这个符号似乎可以很好地工作。。。在cols = {}
cols[1] = {}
cols[1]['A'] = 1
cols[1]['B'] = 2
print cols
>> {'1' : {'A':1,'B':2} }
为什么?在
编辑:
我想我只是需要一双眼睛。问题是这条循环线。。。在cols[ run_number ] = {}
每次到达预先存在的run_number列时,它将cols[run_number]重写为空dict。在