1、字典列表——列表中嵌套字典
先建立字典,再将字典嵌套进列表中,打印出列表
练习:
friend1 = {‘first_name’:‘海王’,‘last_name’:‘张’,‘size’:‘24’}
friend2 = {‘first_name’:‘海后’,‘last_name’:‘王’,‘size’:‘26’}
friends = [friend1,friend2]
for friend in friends:
print(friend)
运行结果:
{‘first_name’: ‘海王’, ‘last_name’: ‘张’, ‘size’: ‘24’}
{‘first_name’: ‘海后’, ‘last_name’: ‘王’, ‘size’: ‘26’}
练习2:
friends = [] #创建空列表
for friend_number in range(20): #自动生成20个friend
new_friend = {‘first_name’:‘海王’,‘last_name’:‘张’,‘size’:‘24’}
friends.append(new_friend) #添加到空列表中
for friend in friends[:5]: #提取列表中前5个字典
print(friend)
print(’……’)
运行结果:
{‘first_name’: ‘海王’, ‘last_name’: ‘张’, ‘size’: ‘24’}
{‘first_name’: ‘海王’, ‘last_name’: ‘张’, ‘size’: ‘24’}
{‘first_name’