简体   繁体   中英

Access element in a Python dictionary

I have this dictionary which contains another dictionary inside:

dic = {'dos': {'int' : {'k' : ['float'], 'g': ['float']}}, 'uno': { 'int': {'k':['float']}}}

It stores the name of a function, its type and the parameters, MY problem is i cant access the type of the parameter, lets say for the function dos i want to get the int from the dictionary I've tried with dic['dos'] which will return {'int' : {'k' : ['float'], 'g': ['float']}} and also tried with dic['dos'].itervalues.next() which return {'k' : ['float'], 'g': ['float']}

Sorry if my explanation isn't clear enough, im fairly new to python and my knowledge of dictionaries and lists is limited

This will do it:

print dic['dos'].keys()[0]  # Prints 'int'

but it's rather odd that you're using the key of a dictionary in this way. Is it possible that this dictionary could have more than one entry? I don't quite follow what your data structure is doing...

Assuming that there is only one key at that level

print next(iter(dic['dos']))

If there's more than one key, you can use

list(dic['dos'])  # returns a list in Python3 too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM