한 번에 여러 사전을 만든 다음 사전을 인쇄하는 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'}}
목록 (인덱서에 "D"를 표시하지 않음) 또는 다른 사전을 사용하십시오. 나중에 고맙다. –
또는 순차 숫자가 아닌 임의로 이름을 지정할 수 있도록하려면 사전 사전을 사용하십시오. 계산 된 키는 여전히 컴퓨터 이름보다 낫습니다. – agf
당신의'number' 루프는 대신 다음과 같아야합니다 :'for range of number (1, 11) : –