一度に複数の辞書を作成してから辞書を出力するPythonスクリプトを書きたいのですが、文字列を変数に変換する方法がわかりません。
number = 0
while (number < 10):
number = number + 1
dictionarynumber = ("D"+str(number)) # creates the dictionarys name(D1,D2,D3,D4,D5,D6,D7,D8,D9,D10)
var(dictionarynumber) = {"Fruit":"Apple","Book":"Oliver Twist","Building":"White House"} #Of course var() doesn't work but I placed it here to get my point across
print(dictionarynumber)
回答後:
私は辞書についての考えが好きで、不要な「D」を取り除くことは理にかなっています。皆さんはこれについてどう思いますか?
dict = {}
n = 0
while (n < 10):
n = n + 1
d = n
d = {"Key":"Information"}
dict[n] = d
print(dict)
# Output =
# {1: {'Key': 'Information'},
# 2: {'Key': 'Information'},
# 3: {'Key': 'Information'},
# 4: {'Key': 'Information'},
# 5: {'Key': 'Information'},
# 6: {'Key': 'Information'},
# 7: {'Key': 'Information'},
# 8: {'Key': 'Information'},
# 9: {'Key': 'Information'},
# 10: {'Key': 'Information'}}