【问题标题】:How to get multiple nested keys outside a list如何在列表外获取多个嵌套键
【发布时间】:2020-09-03 08:50:28
【问题描述】:

我正在尝试打印列表中的一些嵌套键。以下是我的清单。

my_list = [
'url',
 100,
 [{'food1': 'sushi',
   'food2': 'tajine',
   'fruit': {'desert': {'sweet': 'tiramusi'}, 'exotic': 'mango'}},
  {'food1': 'rice',
   'food2': 'tajinev',
   'fruit': {'desert': {'sweet': 'ice-cream'}, 'exotic': 'banana'}},
  {'food1': 'tajine',
   'food2': 'burger',
   'fruit': {'desert': {'sweet': 'cheesecake'}, 'exotic': 'pineapple'}}]]

现在我的目标是打印以下内容:

  1. 我想要所有的exotics fruits
  2. 我想要所有的sweet inside deserts

这是我的代码:

for x in my_list:
    print(x[2][0]['fruit']['exotic']) #<--- these are the exotic fruits
    print(x[2][0]['desert']['sweet']) #<--- these are the sweet deserts

我收到以下错误:TypeError: 'int' object is not subscriptable

我的预期结果是:

mango
banana
pineapple
tiramisu
ice-cream
cheesecake

【问题讨论】:

    标签: python list dictionary printing


    【解决方案1】:

    那是因为你迭代错误的地方。试试这个:

    my_list = [
        ('url'), 100, [
            {'food1': 'sushi', 'food2': 'tajine', 
            'fruit': {'exotic': 'mango', 'desert': {'sweet': 'tiramusi'}}}, 
            {'food1': 'rice', 'food2': 'tajinev', 
            'fruit': {'exotic': 'banana', 'desert': {'sweet': 'ice-cream'}}}, 
            {'food1': 'tajine', 'food2': 'burger', 
            'fruit': {'exotic': 'pineapple', 'desert': {'sweet': 'cheesecake'}}}
        ]
    ]
    
    for x in my_list[2]:
        print(x['fruit']['exotic']) #<--- these are the exotic fruits
        print(x['fruit']['desert']['sweet']) #<--- these are the sweet deserts
    

    【讨论】:

      【解决方案2】:

      这是正确的代码

      # Original List
      
      my_list = [
      ('url'), 100, 
      [{'food1': 'sushi', 'food2': 'tajine', 'fruit': {'exotic': 'mango', 'desert': {'sweet': 'tiramusi'}}}, 
      {'food1': 'rice', 'food2': 'tajinev', 'fruit': {'exotic': 'banana', 'desert': {'sweet': 'ice-cream'}}}, 
      {'food1': 'tajine', 'food2': 'burger', 'fruit': {'exotic': 'pineapple', 'desert': {'sweet': 'cheesecake'}}}]
      ]
      
      # Print All Exotic Fruits
      
      for i in range(len(my_list)):
          
          print(my_list[2][i]['fruit']['exotic'])
      
      # Print all Sweets
          
      for i in range(len(my_list)):
          
          print(my_list[2][i]['fruit']['desert']['sweet'])
      

      【讨论】:

        【解决方案3】:

        您想使用 ['fruit']['desert']['sweet'] 而不是 ['desert']['sweet']。

        x=[
        ('url'), 100, [{'food1': 'sushi', 'food2': 'tajine', 
        'fruit': {'exotic': 'mango', 'desert': {'sweet': 'tiramusi'}}}, 
        {'food1': 'rice', 'food2': 'tajinev', 
        'fruit': {'exotic': 'banana', 'desert': {'sweet': 'ice-cream'}}}, 
        {'food1': 'tajine', 'food2': 'burger', 
        'fruit': {'exotic': 'pineapple', 'desert': {'sweet': 'cheesecake'}}}]
        ]
        
        print(*(map(lambda x:x['fruit']['exotic'],x[2])),sep="\n")
        print(*(map(lambda x:x['fruit']['desert']['sweet'],x[2])),sep="\n")
        

        【讨论】:

          猜你喜欢
          • 2012-12-15
          • 2014-09-10
          • 1970-01-01
          • 1970-01-01
          • 2021-05-10
          • 1970-01-01
          • 1970-01-01
          • 2020-04-15
          • 1970-01-01
          相关资源
          最近更新 更多