python_dict_嵌套value值处理

  1. 嵌套字典值处理
    多层嵌套分解
allGuests = {'Alice': {'apples': 5, 'pretzels': 12}, 
             'Bob': {'ham sandwiches': 3, 'apples': 2},
             'Carol': {'cups': 3, 'apple pies': 1}}
def getnum(guest,item):
    num = 0
    for k,v in allGuests.items():
        num += v.get(item)
        return num
getnum(allGuests,'apples')

猜你喜欢

转载自blog.csdn.net/Katherine_pbq/article/details/86474022
今日推荐