新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:計算字典中項目的和
創(chuàng)新互聯(lián)python教程:

為杭錦等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及杭錦網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、成都做網(wǎng)站、杭錦網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
寫一個 Python 程序,用一個實際的例子找到字典中項目的總和。
在字典中查找項目總和的 Python 程序示例 1
在這個程序中我們使用的是求和函數(shù),字典值函數(shù)求字典值的和。 Python sum 函數(shù)是返回一個字典 中所有值的總和
# Python Program to find Sum of Items in a Dictionary
myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)
# Print Values using get
print("\nSum of Values: ", sum(myDict.values()))計算字典中項目總和的 Python 程序示例 2
這個 Python 程序使用 For 循環(huán)和值函數(shù)在字典中添加值。
myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)
total = 0
# Print Values using get
for i in myDict.values():
total = total + i
print("\nThe Total Sum of Values : ", total)Python 字典項輸出的總和
Dictionary: {'x': 250, 'y': 500, 'z': 410}
The Total Sum of Values : 1160計算字典中所有項目總和的 Python 程序示例 3
在這個 Python 程序中,我們使用 For Loop 來迭代這個字典中的每個元素。在 Python 循環(huán)中,我們將這些字典值添加到總變量中。
myDict = {'x': 250, 'y':500, 'z':410}
print("Dictionary: ", myDict)
total = 0
# Print Values using get
for i in myDict:
total = total + myDict[i]
print("\nThe Total Sum of Values : ", total)Dictionary: {'x': 250, 'y': 500, 'z': 410}
The Total Sum of Values : 1160 新聞名稱:Python 程序:計算字典中項目的和
鏈接URL:http://www.dlmjj.cn/article/dhghgih.html


咨詢
建站咨詢
