当前位置:首页 > Python教程 > python进阶

Python 计算列表元素之积

python 计算列表元素之积

定义一个数字列表,并计算列表元素之积。

例如:

输入 : list1 = [1, 2, 3] 
输出 : 6 
计算:1 * 2 * 3

实例 1

def multiplylist(mylist) :
     
    result = 1
    for x in mylist:
         result = result * x  
    return result  
     
list1 = [1, 2, 3]  
list2 = [3, 2, 4]
print(multiplylist(list1))
print(multiplylist(list2))

以上实例输出结果为:

6
24


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!