【问题标题】:Python dictionary in a dictionary printing字典打印中的 Python 字典
【发布时间】:2017-10-13 19:26:52
【问题描述】:

我正在尝试在 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(key(['random_dic'['couples'['a']]])) 应该是print(random_dic['couples']['a'])
  • 非常感谢,我想再问一个问题,但我还要再等 90 分钟,请问我可以在这里问你吗?
  • @TIC-FLY 如果我的回答解决了您最初的问题,您能否将其标记为已解决?谢谢

标签: python python-3.x python-3.6


【解决方案1】:

将最后一行编辑为:

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

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

【讨论】:

  • 感谢您抽出宝贵时间帮助我。
猜你喜欢
  • 2015-07-05
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
相关资源
最近更新 更多