【发布时间】:2022-01-14 14:58:17
【问题描述】:
我有一个这样的嵌套子列表:
my_list = {'active': {'type': 'boolean', 'string': 'Active'}, 'name': {'type': 'char', 'string': 'Title'}, 'description': {'type': 'html', 'string': 'Description'}, 'priority': {'type': 'selection', 'string': 'Priority'}, 'product': {'type': 'char', 'help': 'Selected product by user', 'string': 'Product'}}
您会注意到子列表的内容并不总是相同:'type'、'string',有时还有'help'。
我可以运行一个基本的for 循环来显示列表的元素:
for i in my_list:
print(i)
返回给我:
active
name
description
priority
product
但是我怎样才能遍历子列表的项目呢?我想我需要嵌套另一个 for 循环。
我想显示具体信息,例如:
- Active: active, boolean
- Title: name, char
- Description: description, html
- Priority: priority, selection
- Product: product, char, Selected product by user
【问题讨论】:
-
那些是字典,不是列表。使用
.items()遍历字典中的键值对 -
您想要的输出与输入不匹配?请添加有关您要显示的内容的更多详细信息
-
for j in my_list[i]:将迭代内部字典
标签: python multidimensional-array