前提・実現したいこと
pythonで辞書型を配列に追加していきたいです.その中で辞書型の["test1"]
の部分だけ連番を振りたいです.以下の問題につてご教授ください.
発生している問題・エラーメッセージ
[{'test1': 0, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 1, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 2, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 3, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}]
上記のような辞書型を格納した配列を作成したいのですが以下の該当のソースコードを実行すると以下の配列が生成されます.
[{'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}, {'test1': 4, 'test2': 2, 'test3': 3, 'test4': 4, 'test5': 5}]
辞書型の["test1"]
が変動せずにrange(5)
の最後の値である4に固定されたものが作成されます.
該当のソースコード
python3.7
1agent = {} 2agent_list = [] 3for i in range(5): 4 agent["test1"] = i 5 agent["test2"] = 2 6 agent["test3"] = 3 7 agent["test4"] = 4 8 agent["test5"] = 5 9 agent_list.append(agent) 10 11print(agent_list)
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/02 14:17