迭代拆分元组时无法填充字典 /w Python
Failed to fill a dictionary when iterating with a splited tuple /w Python
一些帮助将不胜感激,因为当我尝试通过从元组中拆分术语来填充字典时,我不明白追加的内容。
这里是元组:
for i in tpl_walllg:
print(i)
(11001, 43.27)
(11003, 5.13)
(11004, 23.62)
(11005, 5.22)
当我对其进行迭代时,通过拆分术语并放入表格中,它起作用了:
for i in tpl_walllg:
print("Wall {id} / {lg}m".format(id=i[0], lg=i[1]))
Wall 11001 / 43.27m
Wall 11003 / 5.13m
Wall 11004 / 23.62m
Wall 11005 / 5.22m
所以我决定用这种方法来填充一个预成型的字典,元组的第一项是字典的 'root' 键,第二项是受影响的值。
for i in tpl_walllg:
wm[i[0]]['propriétés']['géo']['lg'] = i[1]
迭代有效,但为什么值相同(最后一堵墙)?看看:
for i in tpl_walllg:
print(wm[i[0]]['propriétés']['géo']['lg'])
5.22
5.22
5.22
5.22
并输入表格:
for i in tab_wallID:
print('The wall n°{id} is {lg}m long'.format(id=i, lg=wm[i]['propriétés']['géo']['lg']))
The wall n°11001 is 5.22m long
The wall n°11003 is 5.22m long
The wall n°11004 is 5.22m long
The wall n°11005 is 5.22m long
这里tab_wallID只是一个wallID的列表。
下面是词典楼:
wm = {} # Création d'un dictionnaire (wm : wall matrix)
dic_prop = {'géo': {'ep': float(), # wall tickness
'lg': float(), # wall lenght
'ht': float() # wall height
},
'méca': {'fck': int(),
# characteristic compressive strength of concrete
'fyk': int()
# characteristic yield strength of reinforcement
},
'spatiale': {} # not used yet
}
for i in tab_wallID:
wm[i] = {'propriétés': dic_prop,
'torseurs': dic_comb
}
发生这种情况是因为当您构建 wm
字典时,您指向同一个 dic_prop
对象。这就是为什么在循环时,相同的 属性 字典会随着每个新的 i[1]
.
连续更新
一种解决方法是将构造 wn
的方式更改为字典文字,例如:
for i in tab_wallID:
wm[i] = {'propriétés': {'géo': {'lg': float()}}}
编辑:
另一种方法是每次将字典分配给键时复制 dict_prop
字典,例如:
import copy
for i in tab_wallID:
wm[i] = {'propriétés': copy.deepcopy(dic_prop)}
一些帮助将不胜感激,因为当我尝试通过从元组中拆分术语来填充字典时,我不明白追加的内容。
这里是元组:
for i in tpl_walllg:
print(i)
(11001, 43.27)
(11003, 5.13)
(11004, 23.62)
(11005, 5.22)
当我对其进行迭代时,通过拆分术语并放入表格中,它起作用了:
for i in tpl_walllg:
print("Wall {id} / {lg}m".format(id=i[0], lg=i[1]))
Wall 11001 / 43.27m
Wall 11003 / 5.13m
Wall 11004 / 23.62m
Wall 11005 / 5.22m
所以我决定用这种方法来填充一个预成型的字典,元组的第一项是字典的 'root' 键,第二项是受影响的值。
for i in tpl_walllg:
wm[i[0]]['propriétés']['géo']['lg'] = i[1]
迭代有效,但为什么值相同(最后一堵墙)?看看:
for i in tpl_walllg:
print(wm[i[0]]['propriétés']['géo']['lg'])
5.22
5.22
5.22
5.22
并输入表格:
for i in tab_wallID:
print('The wall n°{id} is {lg}m long'.format(id=i, lg=wm[i]['propriétés']['géo']['lg']))
The wall n°11001 is 5.22m long
The wall n°11003 is 5.22m long
The wall n°11004 is 5.22m long
The wall n°11005 is 5.22m long
这里tab_wallID只是一个wallID的列表。
下面是词典楼:
wm = {} # Création d'un dictionnaire (wm : wall matrix)
dic_prop = {'géo': {'ep': float(), # wall tickness
'lg': float(), # wall lenght
'ht': float() # wall height
},
'méca': {'fck': int(),
# characteristic compressive strength of concrete
'fyk': int()
# characteristic yield strength of reinforcement
},
'spatiale': {} # not used yet
}
for i in tab_wallID:
wm[i] = {'propriétés': dic_prop,
'torseurs': dic_comb
}
发生这种情况是因为当您构建 wm
字典时,您指向同一个 dic_prop
对象。这就是为什么在循环时,相同的 属性 字典会随着每个新的 i[1]
.
一种解决方法是将构造 wn
的方式更改为字典文字,例如:
for i in tab_wallID:
wm[i] = {'propriétés': {'géo': {'lg': float()}}}
编辑:
另一种方法是每次将字典分配给键时复制 dict_prop
字典,例如:
import copy
for i in tab_wallID:
wm[i] = {'propriétés': copy.deepcopy(dic_prop)}