The World Wide Web, restored to 800 pixels of glory.
GeezerVison is showing www.runoob.com — Python 计算列表元素之积 | 菜鸟教程
Python3 实例
定义一个数字列表,并计算列表元素之积。
例如:
输入 : list1 = [1, 2, 3] 输出 : 6 计算:1 * 2 * 3
以上实例输出结果为:
6 24
yuna~ha
211***[email protected]
参考:
from functools import reduce list1 = [1,3,5,6,7] sum = reduce(lambda x,y:x*y,list1) print(sum)
魏书豪
dan***[email protected]
采用递归方法:
def list_product(list_1,size): if size == 0: return 1 else: return list_1[size-1] * list_product(list_1,size - 1) list_1 = [i for i in range(3,6)] #生成列表[3,4,5] print(list_1) print(list_product(list_1,len(list_1)))
取消