如何解决函数中的字典值和列表值
products = {'a': 2,'b': 3,'c': 4,'d': 5,'e': 6,'f': 7,'g': 8}
以及列出每种产品的数量的列表:
amounts = [3,5,1,3,2,0]
不使用函数,我似乎正确了:
products = {'a': 2,'g': 8}
amounts = [3,0]
res_list = []
order = []
for value in products.values():
res_list.append(value)
for i in range(0,len(res_list)):
order.append(amounts[i] * res_list[i])
total = sum(order)
print(res_list)
print(order) #this line and the one above are not really necessary
print(total)
输出:63
但是当我尝试在函数中使用此代码时,我遇到了一些问题。这是我尝试过的:
products = {'a': 2,0]
#order = []
def order(prod):
res_list = []
for value in prod.values():
res_list.append(value)
return res_list
prices = order(products)
print(prices)
def order1(prices):
order =[]
for i in range(0,len(prices)):
order.append(amounts[i] * prices[i])
total = sum(order)
return total
print(order1(prices))
无法按预期方式工作。
感谢我正在学习的所有帮助。
解决方法
直接的问题是您的台词:
total = sum(order)
return total
缩进太多,因此它们位于for
循环内。在函数之外,该错误没有太大关系,因为所有发生的事情是每次迭代都会重新计算总数,但最终值是所使用的值。但是在函数内部,将会发生的是它将在第一次迭代时return
。
减少缩进使其不在for
循环中将解决此问题。
def order1(prices):
order =[]
for i in range(0,len(prices)):
order.append(amounts[i] * prices[i])
total = sum(order)
return total
但是,与此分开的是,您依赖于字典中的顺序,只有在Python 3.7和更高版本中才保证此顺序。如果要允许代码在早期版本的Python上可靠运行,可以使用OrderedDict
。
from collections import OrderedDict
products = OrderedDict([('a',2),('b',3),('c',4),('d',5),('e',6),('f',7),('g',8)])
顺便说一句,您的order
函数是不必要的。如果要将products.values()
(字典值迭代器)转换为列表,只需使用:
prices = list(products.values())
此外,在order1
中,无需建立order
列表并将其求和-您可以使用:
total = 0
for i in range(0,len(prices)):
total += amounts[i] * prices[i]
这可能已经足够了,但是如果您想进一步完善,请查看zip
的用法,并考虑如何将其与{ {1}}和amounts
。
仅压缩products.values()
和amounts
,找到每一对的乘积,然后最后求和
>>> products = {'a': 2,'b': 3,'c': 4,'d': 5,'e': 6,'f': 7,'g': 8}
>>> amounts = [3,5,1,3,2,0]
>>>
>>> sum(i*j for i,j in zip(products.values(),amounts))
63
,
您可以这样做。
products = {'a': 2,'g': 8}
amounts = [3,0]
def order(products,amounts):
res_list = []
order = []
for value in products.values():
res_list.append(value)
for i in range(0,len(res_list)):
order.append(amounts[i] * res_list[i])
total = sum(order)
print(res_list)
print(order) #this line and the one above are not really necessary
print(total)
return(total)
order(products,amounts)
,
假设In home.js:
useEffect(()=>{
setTimeout(()=>{
// change styles for 'frontenddevelopment'
changeStyles1();
},1000)
// changeStyles for 'i love frontend'
changeStyles2()
},[])
和products
中的项目数量相同,您实际上不需要重复两次。
amounts
注意:不能保证字典中各项的顺序,您可能需要研究以更好的方式将产品和金额链接在一起的不同数据结构,即:
products = {'a': 2,0]
def order(products: dict,amounts: list):
total = 0
for idx,(_key,val) in enumerate(products.items()):
total = total + amounts[idx] * val
return total
print(order(products,amounts))
通过这种方式,您可以这样做:
products = {'a': 2,'g': 8}
amounts = {'a': 3,'b': 0,'c': 5,'d': 1,'e': 3,'f': 2,'g': 0}
,
一旦我们准备好了,就让我们看一下numpy吧,因为最后,您只需要dot product prices x amounts
:
import numpy as np
total = np.dot(list(products.values()),amounts)
63
但是,严重的是,我严格地对两个数据集使用 列表或字典,而不要混淆它们,因为这会严重导致它们之间的订单同步问题,即使您使用的是Python 3.7,也要进行上述更改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。