如何在python中的嵌套字典中查找特定键而不使用for循环并直接访问该变量。
我有一个类似于嵌套字典的json文件。我怎么能在其中找到特定的关键字?
data={
u'session': {
u'new': True,
u'sessionId': u'amzn1.echo-api.session.fae41185-1c00-4463-b890-0833c32a2b43',
u'user': {
u'userId': u'amzn1.ask.account.AHEEJXVACGJ2XZV4QRNFSIW7LDHAXEUB7WTZXC6777CLHLS4ITEWD6RMQLNXG5ECXFP3WRAQDH53EZUZ2GOFC52SNZ5NI5L7LXG3HMS6ORA2DNVT3TFYL5N5WE4GTSFM4PS7TGYXXQYCMOCRVBEPXBNHV2G6DPIUP3XPHCSIKJF26SOSZZAD4TRLHJA66JPEVDWJMRBH7AL2WUA'
},
u'application': {
u'applicationId': u'amzn1.ask.skill.ec977129-bcaf-462c-85ce-03c1f7a2b1f8'
}
},
u'version': u'1.0',
u'request': {
u'locale': u'en-IN',
u'timestamp': u'2018-01-12T10:11:46Z',
u'dialogState': u'STARTED',
u'intent': {
u'slots': {
u'product': {
u'name': u'product',
u'confirmationStatus': u'NONE'
},
u'subcategories': {
u'resolutions': {
u'resolutionsPerAuthority': [
{
u'status': {
u'code': u'ER_SUCCESS_MATCH'
},
u'values': [
{
u'value': {
u'name': u'milk',
u'id': u'ecbdb882ae865a07d87611437fda0772'
}
}
],
u'authority': u'amzn1.er-authority.echo-sdk.amzn1.ask.skill.ec977129-bcaf-462c-85ce-03c1f7a2b1f8_Certification.subcategories'
}
]
},
u'name': u'subcategories',
u'value': u'milk',
u'confirmationStatus': u'NONE'
},
u'brand': {
u'resolutions': {
u'resolutionsPerAuthority': [
{
u'status': {
u'code': u'ER_SUCCESS_MATCH'
},
u'values': [
{
u'value': {
u'name': u'nestle',
u'id': u'099e3726deeeb0f04f86d49093eebbb2'
}
}
],
u'authority': u'amzn1.er-authority.echo-sdk.amzn1.ask.skill.ec977129-bcaf-462c-85ce-03c1f7a2b1f8_Certification.brand'
}
]
},
u'name': u'brand',
u'value': u'nestle',
u'confirmationStatus': u'NONE'
},
u'categories': {
u'name': u'categories',
u'confirmationStatus': u'NONE'
},
u'more_n_repeat': {
u'name': u'more_n_repeat',
u'confirmationStatus': u'NONE'
}
},
u'name': u'user_says',
u'confirmationStatus': u'NONE'
},
u'requestId': u'amzn1.echo-api.request.432d4866-f781-418a-88a8-5d2b8d341409',
u'type': u'IntentRequest'
},
u'context': {
u'AudioPlayer': {
u'playerActivity': u'IDLE'
},
u'System': {
u'device': {
u'deviceId': u'amzn1.ask.device.AHJQTUWHP2J7MS7WFE45RNWOO32KO4DPTY4HKNU66YPLG3BOFXRFVA32JEILOKUADFOGC4JCTM4CFOPN6MJP2BEQNF3TPOKCPGLHYEDJBLYS5QOIBPKD7SZALTCVKK2YUP7WWUA5XHC2ATFS4MYLXA7I4UVQ',
u'supportedInterfaces': {
u'AudioPlayer': {
}
}
},
u'application': {
u'applicationId': u'amzn1.ask.skill.ec977129-bcaf-462c-85ce-03c1f7a2b1f8'
},
u'apiAccessToken': u'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjEifQ.eyJhdWQiOiJodHRwczovL2FwaS5hbWF6b25hbGV4YS5jb20iLCJpc3MiOiJBbGV4YVNraWxsS2l0Iiwic3ViIjoiYW16bjEuYXNrLnNraWxsLmVjOTc3MTI5LWJjYWYtNDYyYy04NWNlLTAzYzFmN2EyYjFmOCIsImV4cCI6MTUxNTc1NTUwNiwiaWF0IjoxNTE1NzUxOTA2LCJuYmYiOjE1MTU3NTE5MDYsInByaXZhdGVDbGFpbXMiOnsiY29uc2VudFRva2VuIjpudWxsLCJkZXZpY2VJZCI6ImFtem4xLmFzay5kZXZpY2UuQUhKUVRVV0hQMko3TVM3V0ZFNDVSTldPTzMyS080RFBUWTRIS05VNjZZUExHM0JPRlhSRlZBMzJKRUlMT0tVQURGT0dDNEpDVE00Q0ZPUE42TUpQMkJFUU5GM1RQT0tDUEdMSFlFREpCTFlTNVFPSUJQS0Q3U1pBTFRDVktLMllVUDdXV1VBNVhIQzJBVEZTNE1ZTFhBN0k0VVZRIiwidXNlcklkIjoiYW16bjEuYXNrLmFjY291bnQuQUhFRUpYVkFDR0oyWFpWNFFSTkZTSVc3TERIQVhFVUI3V1RaWEM2Nzc3Q0xITFM0SVRFV0Q2Uk1RTE5YRzVFQ1hGUDNXUkFRREg1M0VaVVoyR09GQzUyU05aNU5JNUw3TFhHM0hNUzZPUkEyRE5WVDNURllMNU41V0U0R1RTRk00UFM3VEdZWFhRWUNNT0NSVkJFUFhCTkhWMkc2RFBJVVAzWFBIQ1NJS0pGMjZTT1NaWkFENFRSTEhKQTY2SlBFVkRXSk1SQkg3QUwyV1VBIn19.DsQSWRNaAlNyzdOyCxaYa3n193RxYJCul0RFc-prCDXxpzIYRxJ6wrZkAS7BCSezK-6hcL8wuiFGdVtQPQk57mL8TBjxwczyc_6vHMjeQEzbcIXGE-xLx-lifNxIpH6w9YQNLavMsLIEEsMP0X1dMXDiBXBAe9A6neb0EdTRi6mdJaUC6Lvx6YnZrY1Xu8scm3vqi47BHKYsGaO508m9vZgVIjmz3st5LHW3Uc5svsK0T3P3aew0iH7K9pzpfm1mRF6POd4jLpyoikdLY4HEDQOC9RSLN4W0SvTw1-y8UEerkQolHaJWQdv86F1dqKjlbaF1zRAmWRVYPPVp7gqFag',
u'user': {
u'userId': u'amzn1.ask.account.AHEEJXVACGJ2XZV4QRNFSIW7LDHAXEUB7WTZXC6777CLHLS4ITEWD6RMQLNXG5ECXFP3WRAQDH53EZUZ2GOFC52SNZ5NI5L7LXG3HMS6ORA2DNVT3TFYL5N5WE4GTSFM4PS7TGYXXQYCMOCRVBEPXBNHV2G6DPIUP3XPHCSIKJF26SOSZZAD4TRLHJA66JPEVDWJMRBH7AL2WUA'
},
u'apiEndpoint': u'https://api.eu.amazonalexa.com'
}
}
}
现在我需要从这本词典中找到品牌价值。 有没有办法在单行代码中检索它。
答案 0 :(得分:2)
我假设你有一个类似的数据结构:
data = {
'a': 42,
'b': {
'ba': 23,
'bb': {
'bba': 420
}
}
}
也许您可以提供一个数据示例。
在嵌套结构中,您可以使用递归来走槽。使用递归,您的代码与数据结构深度无关。
一个简单的实现可能如下所示:
def recursive_lookup(k, d):
if k in d:
return d[k]
for v in d.values():
if isinstance(v, dict):
return recursive_lookup(k, v)
return None
我测试过这种方式:
print('a', recursive_lookup('a', data))
print('ba', recursive_lookup('ba', data))
print('bba', recursive_lookup('bba', data))
代码应该适用于任意深层嵌套的dicts。 (直到达到python解释器的最大递归深度)
答案 1 :(得分:0)
outer_keys = filter(lamba x: my_key in x, my_dict.keys())
result = my_dict[next(outer_keys)][my_key]
答案 2 :(得分:-1)
varName = "orange"
if varName in dict_data:
print "i found " + varName + ":" + dict_data[varName]
else
print varName + " not found"
你正在寻找这样的东西吗?