allGuests = { 'A':{'apples':5,'pretzels':12}, 'B':{'hs':3,'apples':2} } def total(guests,item): num = 0 for k in guests: num = num +guests[k].get(item,0) return num print(str(total(allGuests,'apples')))#计算字典中某个键对应的数量之和