【问题标题】:Pyhon dictionary error: "list index out of range"Python 字典错误:\“列表索引超出范围\”
【发布时间】:2022-08-17 19:45:52
【问题描述】:

我有这本词典:

dict = {
    \'BASE\': [
            {\'Work\': [{\"test\": \"This is a test!\"}]},
            {\'Documents\': []}
            ]
}

当我尝试从这本字典中获取值时

test_content = dict[\"BASE\"][0][\"Work\"][0][\"test\"]

我收到此错误:

IndexError: list index out of range

我怎么解决这个问题?

  • dict 是字典的全局变量,使用别的东西。如果我复制/粘贴您的代码并将 dict 更改为 d 它可以在我的计算机上运行。
  • @user56700 我得到同样的错误:test_content = d[\"BASE\"][0][\"Work\"][0][\"test\"] IndexError: list index out of range
  • 好的,在这里工作正常。

标签: python dictionary


【解决方案1】:

只需将 dict 更改为 dict1,它就可以正常工作。

dict1 = {
    'BASE': [
            {'Work': [{"test": "This is a test!"}]},
            {'Documents': []}
            ]
}
test_content = dict1["BASE"][0]["Work"][0]["test"]

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 2016-01-06
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多