假设我有一个字典 = {'a': 1, 'b': 2'},还有一个列表 = ['a', 'b, 'c', 'd', 'e']。目标是将列表元素添加到字典中,并打印出新的字典值以及这些值的总和。应该像这样:
2 a
3 b
1 c
1 d
1 e
Total number of items: 8
相反,我得到了:
1 a
2 b
1 c
1 d
1 e
Total number of items: 6
到目前为止,我已经做了以下工作:
def addToInventory(inventory, addedItems)
for items in list():
dict.setdefault(item, [])
def displayInventory(inventory):
print('Inventory:')
item_count = 0
for k, v in inventory.items():
print(str(v) + ' ' + k)
item_count += int(v)
print('Total number of items: ' + str(item_count))
newInventory=addToInventory(dict, list)
displayInventory(dict)
非常感谢您的帮助!
for items in list(): dict.setdefault(item, [])
这段代码完全没有意义。 - thefourtheye