網(wǎng)站首頁 編程語言 正文
插入代碼塊
使用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)))
列表乘積計算
使用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)))
原文鏈接:https://blog.csdn.net/qdPython/article/details/126017314
相關推薦
- 2022-07-03 C語言數(shù)組全面詳細講解_C 語言
- 2022-09-06 Python的functools模塊使用及說明_python
- 2022-07-09 python?查看cpu的核數(shù)實現(xiàn)_python
- 2023-05-07 GO中什么情況會使用變量逃逸_Golang
- 2023-04-02 Flutter應用Windows平臺接入實踐詳解_C 語言
- 2022-11-13 Git如何恢復到之前版本_相關技巧
- 2022-10-21 使用react+redux實現(xiàn)彈出框案例_React
- 2022-09-15 Android自定義ViewGroup實現(xiàn)選擇面板_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支