python字典對應(yīng)三個值怎么求和?
對字典的值求和是簡單的操作,假設(shè)字典為
dict1 = {"a": 12, "b": 34, "c": 56}
三個鍵為"a","b","c",三個值為12,34,56
dict1的方法values得到的就是制定元組
dict_values([12, 34, 56])
對其求和即可。
dict1 = {"a": 12, "b": 34, "c": 56}
mysum = 0
for i in dict1.values():
mysum += i
print(mysum)
# 102
簡單的寫,就是
print(sum(dict1.values()))
# 102