0
ユーザからの入力を受けて、辞書のリストを初期化したい。 私はうまく動作する次のブロックがあります。辞書のリストの初期設定
people = []
for p in range(3):
cell = {"name": "","age" : 0, "education" : "","height" : 0}
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
私が持っている問題は、次のブロックが私にとってうまくいかない理由です。私はこのラインを使用する場合、反復の終わりに、私はすべての3回は、私が意味する、最新の入力で初期化されたリストを持っている理由
people = []
cell = {"name": "","age" : 0, "education" : "","height" : 0}
for p in range(0,3):
cell["name"] = input("name:")
cell["age"] = int(input("age:"))
cell["education"] = input("education:")
cell["height"] = float(input("height:"))
people.append(cell)
私は理解していない:
cell["name"] = input("name:")
はいけません以前の値は新しい値に置き換えられましたか?
ここcell = {"name": "","age" : 0, "education" : "","height" : 0}
あなたが辞書を作成し、あなたの第二の例のように(ループの前にこれを行う場合はcell
の下でそれへの参照を保存:
あなたは同じ 'cell'オブジェクトを変更し、' people'リストに追加おきます。 –
密接に関連した注記:http:// stackoverflow。com/questions/240178/python-list-of-changes-across-sublists-unexpectedly –