【问题标题】:Dictionary calls within dictionary字典中的字典调用
【发布时间】:2015-11-08 05:48:46
【问题描述】:

所以我试图在字典中调用字典并将键用作值,但到目前为止,我每次尝试都失败了。这就是我所在的地方,请注意 if 表达式之后的所有打印语句都会失败。

def main():
    print("This program tells you about the animal of your choice")
    animal=input("What animal would you like to look up: ")
    animal=animal.lower()

    d2={
        "lion":{"name":"Lion","species":"Panthera leo",
        "image":"http://en.wikipedia.org/wiki/File:Lion_waiting_in_Namibia.jpg", 
        "fact":"Vulnerable species"},
    "dog":{"name:":"Dog","species":"Canis lupus familiaris", 
        "image":"http://en.wikipedia.org/wiki/File:YellowLabradorLooking_new.jpg",
        "fact":"Common house pet"},
        "hippo":{"name":"Hippo","species":"Hippopotamus amphibius",
                  "image":"https://en.wikipedia.org/wiki/Hippopotamus#/media/File:Hippopotamus_-_04.jpg",
                  "fact":"Erbivorous mammal"},
        "cat":{"name":"Cat","species":"Felis catus",
                "image":"https://en.wikipedia.org/wiki/Cat#/media/File:Cat_poster_1.jpg",
                "fact":"Purring hunters"}
        }
    if animal in d2:
        print(d2(animal["name"]), "is the common name")
        print(d2(animal["species"]), "is its latin name")
        print(d2(animal["image"]), "is a picture of", animal)
        print(d2(animal["fact"]),)
    else:
        print("Not in dicionary, try lion, dog, hippo, or cat")
main()

【问题讨论】:

  • 一些缩进在我粘贴代码时消失了,这就是为什么文本在 d2 中看起来如此混乱
  • dict 元素由[] 访问,而不是()
  • 您是否注意到您使用圆括号而不是方括号来访问字典?
  • 动物周围的括号仍然给出这个错误:TypeError:字符串索引必须是整数

标签: python python-2.7 python-3.x dictionary


【解决方案1】:

当你这样做时:

print(d2(animal["name"]), "is the common name")

表示您将d2 视为一个函数。相反,尝试

animal_dict = d2.get(animal, {})
animal_name = animal_dict.get("name")
print("%s is the common name" % animal_name)

等等……

【讨论】:

  • 在 Python 3 中不需要 .keys()
  • 不同意你的第一个 stmt...如果 d2 中的 animal 与 d2.keys() 中的 animal 相同,并且 d2 中的 animal 是首选且简洁的......
  • 同意! Python2 也一样。我的意思是最好是明确的:)
  • 谢谢!我明白我是如何完全忘记 .get() 函数的。祝你有美好的一天!
  • 它可能不那么明确,但if key in mydict 是首选。
【解决方案2】:

这很简单,很重要。见...

print(d2(animal["name"]), "is the common name")

这意味着“打印调用d2的返回值(将单个参数作为animal中索引"name"的对象)和字符串"is the common name"”。

这根本没有意义,不是吗?您不能使用括号(调用运算符,())来“调用”字典。这对你有意义吗? IHMO,这没有任何意义。

相反,您应该索引带有键animal 的字典。但是,从 cmets 看来发生的情况来看,您尝试过这个......

 d2[animal["name"]]

这意味着:“d2 中的索引处的对象(animal 中的索引处的对象"name")。再一次,这没有任何意义。正确的做法是……

 d2[animal]["name"]

这意味着“d2 中索引animal 中的对象中的索引"name" 中的对象”。现在,这是有道理的!您应该对所有打印语句应用相同的更改模式。

为什么会这样? d2 是一本字典。于是……

x = d2[animal]

表示“将对象存储在索引animal in d2 in x”。那么……

x["name"]

表示“x 中索引 "name" 处的对象”。现在,这对你有意义吗? ;)。

编辑:出于历史原因的误导,(几乎)从不在 Python 2.x 中使用input(),除非您知道自己在做什么!请改用raw_input()(相同的界面)。

我希望这对你有所启发!

【讨论】:

  • 好的,谢谢。但是,使用你的方法让我的名字出现 KeyError ......你确定我可以写 print(d2[animal]["name"])
  • @M.gnus:如果您使用的是 Python2,请使用raw_input(),并且永远不要使用input()(除非您使用的是 Python3)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2011-08-16
  • 1970-01-01
  • 2020-12-15
  • 2011-05-18
  • 1970-01-01
  • 2016-12-21
相关资源
最近更新 更多