【问题标题】:When trying to access a array of values for keys in a dictionary I keep getting "KeyError: (whatever index I try to access)当尝试访问字典中键的值数组时,我不断收到“KeyError:(我尝试访问的任何索引)
【发布时间】:2021-05-17 16:31:02
【问题描述】:

我有这本字典(带有 Selenium 列表元素,其中包含定义一些元素属性的字符串数组):

#The dictionary has this structure.
{0: {0: ['string1', 'string2', 'string3']}, 1: {1: ['string1', 'string2', 'string3']}}

当我打印整个字典时,它具有这种结构并且我没有收到任何错误。 但是当我尝试访问某个键的特定值时(例如上例中的 string1):

myPath\for\the\file\file.py, line in function
    print(myDictionary[0][0])
    KeyError: 0

有人知道为什么它不起作用吗?

【问题讨论】:

  • 密钥是'0' 还是0
  • 调试的第一条规则:打印出问题的对象,看看结构是什么。执行 print(myDictionary) 并查看它的外观。
  • @Epsi95 只是0
  • @ScienceSnake 刚刚做到了。有时我想知道,当我这么笨的时候,我是如何完成能够说话的壮举的。感谢您的帮助。

标签: python arrays selenium dictionary


【解决方案1】:

我曾经使用 Java,但字典不是一个东西(或者至少我没有使用它们),所以在使用它们之前我不知道如何使用它们。 这导致像这样创建字典的元素:

mydictionary[i] = {i: [some attributes]}

代替:

mydictionary[i] = [some attributes]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2023-03-14
    • 2023-02-25
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多