寫一個 Python 程序,用一個實際例子將字典中的所有條目相乘。
Python 程序將字典中的所有項目相乘示例 1
在這個 python 程序中,我們使用 For 循環來迭代字典中的每個元素。在循環的 Python 中,我們將這些值乘以總變量。
# Python Program to Multiply All Items in a Dictionary
myDict = {'x': 20, 'y':5, 'z':60}
print("Dictionary: ", myDict)
total = 1
# Multiply Items
for key in myDict:
total = total * myDict[key]
print("\nAfter Multiplying Items in this Dictionary: ", total)
在字典中增加項目的 Python 程序示例 2
這個 python 程序使用 For Loop 和值函數來相乘字典中的值。
# Python Program to Multiply All Items in a Dictionary
myDict = {'x': 2, 'y':50, 'z':70}
print("Dictionary: ", myDict)
total = 1
# Multiply Items
for i in myDict.values():
total = total * i
print("\nAfter Multiplying Items in this Dictionary: ", total)
倍增 Python 字典項輸出
Dictionary: {'x': 2, 'y': 50, 'z': 70}
After Multiplying Items in this Dictionary: 7000
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247189.html