新聞中心
創(chuàng)新互聯(lián)python教程:

為杭錦等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及杭錦網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、成都做網(wǎng)站、杭錦網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
寫一個(gè) Python 程序,用一個(gè)實(shí)際的例子找到字典中項(xiàng)目的總和。
在字典中查找項(xiàng)目總和的 Python 程序示例 1
在這個(gè)程序中我們使用的是求和函數(shù),字典值函數(shù)求字典值的和。 Python sum 函數(shù)是返回一個(gè)字典 中所有值的總和
# 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()))計(jì)算字典中項(xiàng)目總和的 Python 程序示例 2
這個(gè) 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 字典項(xiàng)輸出的總和
Dictionary: {'x': 250, 'y': 500, 'z': 410}
The Total Sum of Values : 1160計(jì)算字典中所有項(xiàng)目總和的 Python 程序示例 3
在這個(gè) Python 程序中,我們使用 For Loop 來(lái)迭代這個(gè)字典中的每個(gè)元素。在 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 程序:計(jì)算字典中項(xiàng)目的和
標(biāo)題鏈接:http://fisionsoft.com.cn/article/dhghgih.html


咨詢
建站咨詢
