我在试我的代码。我很困惑。如何组合这两个字典,使结果的值与预期值相同? 对于每一张这样的图表和总价,如下所示:
[{'Cherries': 1, 'Blueberries': 2, 'Banana': 3, 'Avocado': 2, 'Blackberries': 2}, {'Apple': 6, 'Avocado': 5, 'Banana': 3, 'Blackberries': 10, 'Blueberries': 12, 'Cherries': 7, 'Date Fruit': 14, 'Grapes': 15, 'Guava': 8, 'Jackfruit': 7, 'Kiwifruit': 9}]
output 70
[{'Cherries': 4, 'Blackberries': 2, 'Avocado': 2, 'Blueberries': 2}, {'Apple': 6, 'Avocado': 5, 'Banana': 3, 'Blackberries': 10, 'Blueberries': 12, 'Cherries': 7, 'Date Fruit': 14, 'Grapes': 15, 'Guava': 8, 'Jackfruit': 7, 'Kiwifruit': 9}]
output 82
[{'Avocado': 1, 'Blueberries': 2, 'Cherries': 3, 'Banana': 2, 'Apple': 1, 'Blackberries': 1}, {'Apple': 6, 'Avocado': 5, 'Banana': 3, 'Blackberries': 10, 'Blueberries': 12, 'Cherries': 7, 'Date Fruit': 14, 'Grapes': 15, 'Guava': 8, 'Jackfruit': 7, 'Kiwifruit': 9}]
output 72
对于我的代码:
fruits = ['Apple','Avocado','Banana','Blackberries','Blueberries','Cherries','Date Fruit','Grapes','Guava','Jackfruit','Kiwifruit']
prices = [6,5,3,10,12,7,14,15,8,7,9]
chart = ['Blueberries','Blueberries','Grapes','Apple','Apple','Apple','Blueberries','Guava','Jackfruit','Blueberries','Jackfruit']
d1 = dict(zip(fruits,prices))
fruit_price = None
dcounter = {}
for i in chart:
if i in dcounter:
dcounter[i] +=1
else:
dcounter[i] =1
#print(dcounter)
fruit_price = {}
for i, j in d1.items():
for x, y in dcounter.items():
if i == x:
fruit_price[i]=(j*y)
#print(fruit_price)
def total_price(dcounter,fprice):
total = 0
for i in fruit_price.values():
total+= i
return total
total_price(dcounter,fruit_price)
我想为数据计数器和水果价格输入函数(定义总价)编码 请帮帮我。谢谢
你的问题一点也不清楚,然而,我设法理解了这个问题
首先,您的输入包含两个字典,一个是
shopping_list
,另一个是prices
所以,首先,先想一想,为了清晰起见,将两者提取出来
现在,可以很容易地遍历购物清单,得到您购买的物品的数量,然后将该数量乘以物品的价格
现在过程已经清楚了,您可以在函数体中添加第一步,并向其传递单个输入
相关问题 更多 >
编程相关推荐