【发布时间】:2011-04-20 01:57:47
【问题描述】:
如果我有一个值,以及我想要乘以该值的附加项列表:
n = 10
terms = [1,2,3,4]
是否可以使用列表推导来做这样的事情:
n *= (term for term in terms) #not working...
或者是唯一的方法:
n *= reduce(lambda x,y: x*y, terms)
这是在 Python 2.6.2 上。谢谢!
【问题讨论】:
标签: python list-comprehension reduce