I'm having a hard time returning the values of each instance of $t
in the nested dictionary below. What I need to do is pull each of the key-value pairs and add them individually to another dictionary.
我很难在下面的嵌套字典中返回$ t的每个实例的值。我需要做的是拉出每个键值对并将它们分别添加到另一个字典中。
Here's the JSON:
这是JSON:
"breed": [
{
"$t": "Chihuahua"
},
{
"$t": "Jack Russell Terrier"
}
]
By the way, I'm using Python 2.7
顺便说一句,我使用的是Python 2.7
3 个解决方案
#1
1
Something like this?
像这样的东西?
>>> o = [ { "$t": "Chihuahua" }, { "$t": "Jack Russell Terrier" } ]
>>> [ item["$t"] for item in o ]
['Chihuahua', 'Jack Russell Terrier']
>>>
#2
0
Is this what you are looking for? (It depends I think on how you want to handle the multiple values corresponding to the same $t
.)
这是你想要的? (这取决于我想你如何处理对应于同一$ t的多个值。)
nestedDict = { "breed": [
{
"$t": "Chihuahua"
},
{
"$t": "Jack Russell Terrier"
}
]
}
dictEntries = [ (k, v) for dicList in nestedDict.values() for d in dicList for (k, v) in d.items() ]
flattenedDict = { }
for k, v in dictEntries:
flattenedDict.setdefault( k, [] ).append( v )
print ( flattenedDict )
This gives you:
这给你:
{'$t': ['Chihuahua', 'Jack Russell Terrier']}
#3
0
I don't understood what are you trying to do. If you want to create a Python dict from JSON ans get its values with the "$t" key, here it is (if not, comment and I delete the answer).
我不明白你想做什么。如果你想从JSON创建一个Python dict,用“$ t”键获取它的值,这里是(如果不是,注释,我删除答案)。
# Many thanks to Dogbert, whose answer I copied the list comprehension from
# (changing a few things), and many thanks to slothrop, whose answer gave me
# ideas for my variable name. Not for those people, I would have used a silly
# name like `thing` and would have used a for loop.
import json
nested_dict = json.loads('{"breed": [{"$t": "Chihuahua"}, '
'{"$t": "Jack Russell Terrier"}]}')
[dic["$t"] for dic in nested_dict["breed"]]
If you need the key-value pairs of every dict inside your dict:
如果你需要你的dict中每个dict的键值对:
key_and_value_pairs = []
for dic in nested_dict["breed"]:
key_and_value_pairs.extend(dic.items())
#1
1
Something like this?
像这样的东西?
>>> o = [ { "$t": "Chihuahua" }, { "$t": "Jack Russell Terrier" } ]
>>> [ item["$t"] for item in o ]
['Chihuahua', 'Jack Russell Terrier']
>>>
#2
0
Is this what you are looking for? (It depends I think on how you want to handle the multiple values corresponding to the same $t
.)
这是你想要的? (这取决于我想你如何处理对应于同一$ t的多个值。)
nestedDict = { "breed": [
{
"$t": "Chihuahua"
},
{
"$t": "Jack Russell Terrier"
}
]
}
dictEntries = [ (k, v) for dicList in nestedDict.values() for d in dicList for (k, v) in d.items() ]
flattenedDict = { }
for k, v in dictEntries:
flattenedDict.setdefault( k, [] ).append( v )
print ( flattenedDict )
This gives you:
这给你:
{'$t': ['Chihuahua', 'Jack Russell Terrier']}
#3
0
I don't understood what are you trying to do. If you want to create a Python dict from JSON ans get its values with the "$t" key, here it is (if not, comment and I delete the answer).
我不明白你想做什么。如果你想从JSON创建一个Python dict,用“$ t”键获取它的值,这里是(如果不是,注释,我删除答案)。
# Many thanks to Dogbert, whose answer I copied the list comprehension from
# (changing a few things), and many thanks to slothrop, whose answer gave me
# ideas for my variable name. Not for those people, I would have used a silly
# name like `thing` and would have used a for loop.
import json
nested_dict = json.loads('{"breed": [{"$t": "Chihuahua"}, '
'{"$t": "Jack Russell Terrier"}]}')
[dic["$t"] for dic in nested_dict["breed"]]
If you need the key-value pairs of every dict inside your dict:
如果你需要你的dict中每个dict的键值对:
key_and_value_pairs = []
for dic in nested_dict["breed"]:
key_and_value_pairs.extend(dic.items())