私は線形計画問題で作業する変数のリストを設定しようとしています。このためには、コードを大幅に短くて読みやすくするために、いくつかのインデックス値を使用したいと思います。これは私のリストを返しますPython3.5で変数を索引付けし、変数名に月を追加
from datetime import *
months = ["Unknown", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
d={}
for x in range(1,13):
d["string{0}".format(x)]=("Production in months[d]")
print(d)
:
{'string7': 'Production in months[d]', 'string8': 'Production in months[d]', 'string2': 'Production in months[d]', 'string9': 'Production in months[d]', 'string11': 'Production in months[d]', 'string6': 'Production in months[d]', 'string12': 'Production in months[d]', 'string3': 'Production in months[d]', 'string10': 'Production in months[d]', 'string4': 'Production in months[d]', 'string1': 'Production in months[d]', 'string5': 'Production in months[d]'}
私はインデックス番号に対応し、数ヶ月[D]が印刷された月の名前を持っていると思います私がように何かをしようとしたのです私は 'string [i]'にいます。
'd {" string {0} "。format(x)" = "{}"形式での書式(月[x]) 'または' d {"文字列{0}" .format(x)] = "Production in" + months [x] –
ありがとうございました!トリックをしました – Jeroen
私の答えを[受け入れ](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)としてマークしました。 –