在嵌套词典中查找一个值,以在单独的词典中获得一个值
在来自API请求的响应中。我得到了一种嵌套词典列表的格式。例如。
{
"data": [
{
"3": {
"value": 1
},
"6": {
"value": "Conversion"
},
"7": {
"value": "HVAC"
}
},
我可以使用r ['data']轻松地超越第一个字典。此时,每个列表项目都是数据库中的记录。 r ['data'] [0]给了我一个字典的字典('3'),然后是一个值为什么的字典({'value':'conversion'})。
我希望能够查找像“转换”之类的值,并告诉我字段“ 3”的价值。无论如何要使用Python做到这一点?
In the response that comes back from an API request. I get a format that is a list of nested dictionaries. for example.
{
"data": [
{
"3": {
"value": 1
},
"6": {
"value": "Conversion"
},
"7": {
"value": "HVAC"
}
},
I can easily get past the the first dictionary using r['data']. At this point, each list item is a record in a database. r['data'][0] gives me a dictionary of what the field ('3') is and then a dictionary of what the value is ({'value': 'Conversion'}).
I want to be able to look up a value like 'Conversion' and have it tell me what the value is for field '3'. Anyway to do this using python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的描述不太适合现实。让我们拥有一个完整的结构:
r 是词典。它包含一个单个键('data'),其关联值是一个列表,在此示例中包含一个字典。该词典有3个键 - “ 3”,“ 6”和“ 7”。这些键中的每个键都有一个本身是由单个键('value')和相关值组成的字典。
您可以断言如下:
希望您可以访问较低级别的值(s)
不清楚的问题是,当您想要从键'3'中的值时,您为什么要搜索“转换”,这将是:
<强>编辑:
以这种方式这样做提供了指定相关键和值的灵活性
Your description doesn't quite fit with reality. Let's have a complete structure:
r is a dictionary. It contains a single key ('data') whose associated value is a list which, in this example, contains one dictionary. That dictionary has 3 keys - "3", "6" and "7". Each of those keys has a value which itself is a dictionary comprised of a single key ('value') and, obviously, an associated value.
You can assert as follows:
Hopefully that shows how you can access the lower level value(s)
What's unclear from your question is why would you be searching for 'Conversion' when you want the value from key '3' which would be:
EDIT:
Doing it this way offers the flexibility of specifying the relevant keys and value
我必须假设您是尝试要说的是您需要基于值索引。
这将返回'[0] [6]'。我不知道您的数据是否应该以这种方式格式化...“数据”中的列表,然后在内部只有一个。但是,鉴于这种格式,这将为您提供索引。
I have to make an assumption that what you are trying to say is that you want the index based on the value.
This returns '[0][6]'. I don't know if your data is supposed to be formatted that way... a list within 'data' and then only one dict inside. But, given this format, this will give you the index.