最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python計(jì)算列表元素與乘積詳情

 更新時(shí)間:2022年08月04日 14:10:00   作者:Python熱愛者  
這篇文章主要介紹了python計(jì)算列表元素與乘積,文章圍繞主題展開詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

插入代碼塊

使用sum函數(shù):

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(sum(numbers))

使用reduce函數(shù):

# 方式1
from functools import reduce

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
results = reduce(lambda x, y: x + y, numbers)
print(results)

# 方式2
from operator import add
from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
results = reduce(add, numbers)
print(results)

使用for循環(huán):

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = 0
for number in numbers:
    result += number
print(result)

使用遞歸:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def cal(list1, size):
    if size:
        return list1[size - 1] + cal(list1, size - 1)
    return size
print(cal(numbers, len(numbers)))

列表乘積計(jì)算

使用for循環(huán):

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = 1
for number in numbers:
    result *= number
print(result)

使用reduce函數(shù):

# 方式1
from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
results = reduce(lambda x, y: x * y, numbers)
print(results)

# 方式2
from operator import mul
from functools import reduce
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
results = reduce(mul, numbers)
print(results)

使用遞歸函數(shù):

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def cal(list1, size):
    if size == 0:
        return 1
    return list1[size - 1] * cal(list1, size - 1)
print(cal(numbers, len(numbers)))

到此這篇關(guān)于python計(jì)算列表元素與乘積的文章就介紹到這了,更多相關(guān)python計(jì)算列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

湾仔区| 娄烦县| 金平| 蒙城县| 文登市| 福泉市| 汉中市| 邓州市| 林周县| 延津县| 什邡市| 天门市| 民县| 卫辉市| 西盟| 嘉鱼县| 尼勒克县| 舟曲县| 聂拉木县| 南开区| 凤凰县| 平乡县| 新泰市| 明水县| 贵南县| 庆阳市| 双鸭山市| 六枝特区| 新巴尔虎右旗| 开封市| 苗栗市| 迭部县| 德惠市| 凤庆县| 宝坻区| 苍南县| 许昌县| 靖安县| 雅安市| 贞丰县| 涞源县|