列表嵌套字典问题
kitty = {'species':'cat','owner':'Lucy'}
tide = {'species':'bear',
'owner':'Jim'}
black = {'species':'dog',
'owner':'owen'}
woody = {'species':'bird',
'owner':'Li Lei'}
pets =
for pet in pets:
for sp ,ow in pet.items():
print ('\nspecies: '+ pet['species'])
print ('owner: '+ pet['owner'])
运行出来:
species: cat
owner: Lucy
species: cat
owner: Lucy
species: bear
owner: Jim
species: bear
owner: Jim
species: dog
owner: owen
species: dog
owner: owen
species: bird
owner: Li Lei
species: bird
owner: Li Lei
问题一:为什么每一个字典都打印了两次?我该怎样修改。
问题二:我想在每一个字典前面打印出列表名,该怎样做
你字典有2对键值,循环2次,第二个循环不要就行了
你打印哪个列表名
页:
[1]