Accessing elements of Python dictionary by index

后端 未结 10 1701
说谎
说谎 2020-11-22 08:52

Consider a dict like

mydict = {
  \'Apple\': {\'American\':\'16\', \'Mexican\':10, \'Chinese\':5},
  \'Grapes\':{\'Arabian\':\'25\',\'Indian\':\'20\'} }
         


        
10条回答
  •  醉话见心
    2020-11-22 09:09

    Given that it is a dictionary you access it by using the keys. Getting the dictionary stored under "Apple", do the following:

    >>> mydict["Apple"]
    {'American': '16', 'Mexican': 10, 'Chinese': 5}
    

    And getting how many of them are American (16), do like this:

    >>> mydict["Apple"]["American"]
    '16'
    

提交回复
热议问题