问题
I have a list within a dictionary within a dictionary:
{FirmA:{ProductA:[Color1,Color2,Color3]}}
I want to build a list of keys from the First Firm dictionary level.
Then, I need to access the second level Product dictionary based on a Firm Key.
Finally, I will need to access the Colors list based on the Product key from Dictionary level 2 (Products).
I tried to get the level 1 keys for Firms:
[i for i in dict.keys()]
Returns
ValueError: Too many values to unpack
This is a fairly large data set.
I have not been able to get to the 2nd level dictionary yet.
回答1:
Something like this should get you started:
def get_products_for_firm(d, firm):
firm_letter = firm[-1]
product_prefix = "Product"
product_key = "%s%s" % (product_prefix, firm_letter)
return d[firm][product_key]
d = {
"FirmA": {
"ProductA": ["Color1", "Color2", "Color3"]
},
"FirmB": {
"ProductB": ["Color4", "Color5", "Color6"]
}
}
firm_keys = d.keys()
print "The firm keys are %s: " % (firm_keys)
for firm_key in firm_keys:
print "The products for %s are: %s" % (firm_key,
get_products_for_firm(d, firm_key))
Output:
The firm keys are ['FirmA', 'FirmB']:
The products for FirmA are: ['Color1', 'Color2', 'Color3']
The products for FirmB are: ['Color4', 'Color5', 'Color6']
回答2:
What about that:
d = {'foo':{'bar':42}}
# you can do much like for nested list, like so:
print(d['foo'])
print(d['foo']['bar'])
# or you can iterate:
for k,v in d.items():
print(k,v)
# if the value is also a dictionary, iterate on it again:
try:
for k2, v2 in v.items():
if isinstance(v2, list):
for el in v2:
print el
except:
pass
EDIT: actually if it's a large dataset, and you'll have few values below the first pass actually a dictionary, it may be faster to do an instance check as well (isinstance(v, dict)
), since catching is expensive. Depends on the details....
来源:https://stackoverflow.com/questions/57211388/how-can-i-get-the-keys-from-a-nested-dictionary