我正在完成一项任务,我必须打印出地址簿的内容,每个项目按顺序编号。地址簿是嵌套字典:
addressBook = {'Jim': {'name': 'James Marsh', 'address': '32 Morris Ave',
'phone': '6549873217'},
'Leanne': {'name': 'Leanne Moss', 'address': '37 Shamrock Lane',
'phone': '1234567890'},
'Chris': {'name': 'Christopher Philips', 'address': '49 Langley Court',
'phone': '3216549870'}}
我目前列出内容的功能如下:
def listAll(addressBook):
for key, value in addressBook.items():
print(key)
print(value['name'])
print(value['address'])
print(value['phone'])
输出
Jim
James Marsh
32 Morris Ave
654-987-3217
Leanne
Leanne Moss
37 Shamrock Lane
123-456-7890
Chris
Christopher Philips
49 Langley Court
321-654-9870
分配规范说明它们必须按顺序编号,如
1 Jim
James Marsh
32 Morris Ave
654-987-3217
2 Leanne
Leanne Moss
37 Shamrock Lane
123-456-7890
3 Chris
Christopher Philips
49 Langley Court
321-654-9870
我已经尝试了
for index, key, value in enumerate(addressBook.items()):
print(str(index+1) + '\t' + key)
print(value['name'])
print(value['address'])
print(value['phone'] + '\n')
但是会出现ValueError: not enough values to unpack (expected 3, got 2)
错误。我该如何解决这个问题?
答案 0 :(得分:1)
您可以通过以下方式解压缩:
for index, (key, value) in enumerate(addressBook.items()):