【发布时间】: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'}}]]
现在我的目标是打印以下内容:
- 我想要所有的
exotics fruits - 我想要所有的
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