Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用通配符在嵌套字典中查询项_Python_Python 3.x_Dictionary_Nested_Wildcard - Fatal编程技术网

Python 使用通配符在嵌套字典中查询项

Python 使用通配符在嵌套字典中查询项,python,python-3.x,dictionary,nested,wildcard,Python,Python 3.x,Dictionary,Nested,Wildcard,我正在尝试检索嵌套字典中的项目,并将它们打印出来,用于一个使用Python 3和通配符编码的文本游戏 这是字典: dict = { "Ninja1": { "no": "there is a map under the blue rock" }, "amy": { "yes": "the peasant's name is Ato" } } 我想在字典中循环并打印字符串(比如:'有一张地图…),如果它的键是'yes' for ke

我正在尝试检索嵌套字典中的项目,并将它们打印出来,用于一个使用Python 3和通配符编码的文本游戏

这是字典:

dict = {
    "Ninja1": {
        "no": "there is a map under the blue rock"
    },
    "amy": {
        "yes": "the peasant's name is Ato"
    }
}
我想在字典中循环并打印字符串(比如:'有一张地图…),如果它的键是'yes'

for key in dict:
    if dict[WILDCARD] == 'yes':
        print (dict[WILDCARD]['yes'])
我在这方面是新手,所以我相信代码很可怕。
任何帮助都将不胜感激

您需要做的就是测试是否存在密钥;在操作符中使用

for key in yourdict:
    if 'yes' in yourdict[key]:
        print(yourdict[key]['yes'])

你所需要做的就是测试是否有钥匙;在
操作符中使用

for key in yourdict:
    if 'yes' in yourdict[key]:
        print(yourdict[key]['yes'])

你所需要做的就是测试是否有钥匙;在
操作符中使用

for key in yourdict:
    if 'yes' in yourdict[key]:
        print(yourdict[key]['yes'])

你所需要做的就是测试是否有钥匙;在
操作符中使用

for key in yourdict:
    if 'yes' in yourdict[key]:
        print(yourdict[key]['yes'])

不要使用
dict
作为名称;您正在屏蔽内置类型。不要使用
dict
作为名称;您正在屏蔽内置类型。不要使用
dict
作为名称;您正在屏蔽内置类型。不要使用
dict
作为名称;对Martijn Pieters说:有趣的是,你的化身是一个忍者,而我的文字游戏里有忍者哈哈。谢谢你的密码工作得很有魅力!对Martijn Pieters说:有趣的是,你的头像是一个忍者,而我的文字游戏里有忍者哈哈。谢谢你的密码工作得很有魅力!对Martijn Pieters说:有趣的是,你的头像是一个忍者,而我的文字游戏里有忍者哈哈。谢谢你的密码工作得很有魅力!对Martijn Pieters说:有趣的是,你的头像是一个忍者,而我的文字游戏里有忍者哈哈。谢谢你的密码工作得很有魅力!