Python字典中字典打印

Python dictionary in a dictionary printing

我正在尝试在 random_dict 中打印成对 'a' 的值,但出现错误 "TypeError: string indices must be integers",

代码:

coupleascore = 20
couplebscore = 23
couplecscore = 25

judge1scores = [1, 3, 5]
judge2scores = [5, 2, 10]
judge3scores = [7, 5, 9]

random_dic = {'couples': {'a': coupleascore, 'b': couplebscore, 'c': couplecscore} ,'judges': {'1': judge1scores, '2': judge2scores, '3': judge3scores}}

print(key(['random_dic'['couples'['a']]]))

将你的最后一行编辑为:

print(random_dic['couples']['a'])

https://docs.python.org/3/tutorial/datastructures.html#dictionaries 有关字典数据结构的更多信息