字典列表,即在列表中嵌套字典:
dict_0 ={'color':'green','points':5}
dict_1 ={'color':'yellow','points':10}
dict_2 ={'color':'red','points':15}
lists =[dict_0, dict_1, dict_2]
for dict in lists:
print(dict)
输出:
{'color':'green','points':5}
{'color':'yellow','points':10}
{'color':'red','points':15}
字典推导式:
格式:
{key:value for variable in iterable [if expression]}
执行步骤:
- 1、for 循环:遍历可迭代对象,将其值赋给变量。
- 2、if 语句:筛选满足 if 条件的结果。
- 3、存储结构:以键值对的方式存储在字典中。