将两个不同字典中的值相乘



我有两个字典,我需要将它们相乘并得到它们的总和,这两个字典都有相同的键(我需要一些项目的库存和价格的总和)。

# Create a list "menu" with 4 items
menu = ["sandwich", "burger", "fish", "chips"]
# Create a dictionary "stock" with the stock value for each item in the menu
stock = {"sandwich" : 6, "burger" : 5, "fish" : 6, "chips" : 10}
# Create a dictionary "price" with the price for each item in the menu
price = {"sandwich" : 4.50, "burger" : 6.00, "fish" : 6.50, "chips" : 3.50}
# Get the values from stock and price for each menu item and multiply by eachother
for key in price:
menu_stock = price[key] * stock[key]
# Get the sum of these values
total_stock_worth = sum(menu_stock)
# Print statement with the calculated total stock worth
print("The total stock worth is £" + str("{:.2f}".format(total_stock_worth)))

我得到错误消息(对于第12行:total_stock_worth=sum(menu_stock)):TypeError:"float"对象不是可迭代的

我想要的输出是:总股票价值为131.00英镑

menu_stock(在循环中)存储float值,而sum函数要求其参数为可迭代
因此,在计算它们的和之前,您需要累积price*stocks的所有乘积。

sum函数与iterable一起工作,例如:list, set等。在您的代码中,menu_stockfloat,而不是iterable。要解决您的问题,请将menu_stock声明为列表,并将append声明为stockprice的乘积。循环结束后,调用sum函数来计算total_stock_worth

此外,您不需要调用str()方法,format()会自动为您执行此操作。

解决方案

# Create a list "menu" with 4 items
menu = ["sandwich", "burger", "fish", "chips"]
# Create a dictionary "stock" with the stock value for each item in the menu
stock = {"sandwich" : 6, "burger" : 5, "fish" : 6, "chips" : 10}
# Create a dictionary "price" with the price for each item in the menu
price = {"sandwich" : 4.50, "burger" : 6.00, "fish" : 6.50, "chips" : 3.50}
# declare menu_stock as list
menu_stock = []
# Get the values from stock and price for each menu item and multiply by eachother
for key in price:
menu_stock.append(price[key] * stock[key])
# Get the sum of these values
total_stock_worth = sum(menu_stock)
# Print statement with the calculated total stock worth
print("The total stock worth is £{:.2f}".format(total_stock_worth))

要计算total_stock_worth,请尝试:

# Create a list "menu" with 4 items
menu = ["sandwich", "burger", "fish", "chips"]
# Create a dictionary "stock" with the stock value for each item in the menu
stock = {"sandwich": 6, "burger": 5, "fish": 6, "chips": 10}
# Create a dictionary "price" with the price for each item in the menu
price = {"sandwich": 4.50, "burger": 6.00, "fish": 6.50, "chips": 3.50}
# Get the values from stock and price for each menu item and multiply by eachother
total_stock_worth = sum(price[m] * stock[m] for m in menu)
# Print statement with the calculated total stock worth
print("The total stock worth is £" + str("{:.2f}".format(total_stock_worth)))

打印:

The total stock worth is £131.00

您有两个选项:

  • 使用for循环,并用+=递增total_stock_worth;或
  • sum( )与生成器表达式一起使用

在你的代码中,你试图混合两者,但失败的原因有两个:

  • 您对单个值而不是一系列值调用sum
  • 每次写入total_stock_worth = ...时,都会擦除之前存储在total_stock_worth中的值

下面,两个选项:

# Create a list "menu" with 4 items
menu = ["sandwich", "burger", "fish", "chips"]
# Create a dictionary "stock" with the stock value for each item in the menu
stock = {"sandwich" : 6, "burger" : 5, "fish" : 6, "chips" : 10}
# Create a dictionary "price" with the price for each item in the menu
price = {"sandwich" : 4.50, "burger" : 6.00, "fish" : 6.50, "chips" : 3.50}
# OPTION 1: USE A FOR-LOOP AND INCREMENT MANUALLY
total_stock_worth = 0
for key in price:
menu_stock = price[key] * stock[key]
total_stock_worth = total_stock_worth + menu_stock
print("The total stock worth is £{:.2f}".format(total_stock_worth))
# OPTION 2: USE SUM() WITH A GENERATOR-EXPRESSION
total_stock_worth = sum(price[key] * stock[key] for key in price)
print("The total stock worth is £{:.2f}".format(total_stock_worth))

两个选项的输出相同:

The total stock worth is £131.00

根据个人喜好,您可以替换:

total_stock_worth = total_stock_worth + menu_stock

带有:

total_stock_worth += menu_stock

其中CCD_ 26可以被读取为"0";增量";;这两条线实际上是等价的。

相关内容

  • 没有找到相关文章

最新更新