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

wpcxdonn  于 2023-01-04  发布在  Python
关注(0)|答案(4)|浏览(273)

我有两个字典,我需要相乘,并得到总数,都具有相同的关键字(我需要的股票和价格的一些项目的总数)。

# 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行:总库存价值=总和(菜单库存)):TypeError:“float”对象不可迭代
我追求的输出是:股票总价值为131.00英镑

vjrehmav

vjrehmav1#

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))
rqcrx0a6

rqcrx0a62#

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

qnyhuwrf

qnyhuwrf3#

要计算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
yhxst69z

yhxst69z4#

您有两种选择:

  • 使用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 £" + str("{:.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 £" + str("{:.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

其中+=可读作"增量";这两条线其实是等价的。

相关问题