最新国产好看的视频,伊人天堂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)論

黑山县| 黄大仙区| 哈密市| 宣城市| 贵溪市| 东丰县| 山西省| 裕民县| 会东县| 沂水县| 苏尼特左旗| 康定县| 福州市| 高雄市| 巩义市| 平度市| 疏附县| 萨迦县| 中牟县| 合水县| 岳西县| 永德县| 双鸭山市| 凤城市| 南岸区| 太仆寺旗| 井研县| 三河市| 汉寿县| 甘肃省| 富锦市| 光山县| 永善县| 读书| 涟水县| 呼和浩特市| 武宣县| 讷河市| 尉氏县| 阳春市| 新乐市|