網站首頁 編程語言 正文
前言
推導式是一種獨特的數據處理方式,可以快速的從一個數據序列構建另一個新的數據序列的結構體。常用的推導式有一下四種:
- 列表推導式
- 元組推導式
- 集合推導式
- 字典推導式
1、列表推導式
# coding:utf-8
# Author:Yang Xiaopeng
"""
語法格式
[表達式 for 變量 in 變量]
[表達式 for 變量 in 變量 if 條件表達式]
上述格式中 的 表達式中的變量與for變量一致
"""
old_list = [1, 2, 3, 4, 5]
new_list = [new_list * new_list for new_list in old_list] # yes [1, 4, 9, 16, 25]
# new_list = [new_list1 * new_list for new_list in old_list] # NameError: name 'new_list1' is not defined
# new_list = [new_list * new_list for new_list2 in old_list] # NameError: name 'new_list' is not defined
old_list = [old_list * old_list for old_list in old_list] # yes [1, 4, 9, 16, 25]
print(old_list)
print(new_list)
new_list = [old_list for old_list in old_list if old_list%2 == 1] # yes [1, 9, 25]
print(new_list)
2、元組推導式
# coding:utf-8
# Author:Yang Xiaopeng
"""
語法格式
(表達式 for 變量 in 變量)
(表達式 for 變量 in 變量 if 條件表達式)
上述格式中 的 表達式中的變量與for變量一致
"""
old_list = (1, 2, 3, 4, 5)
new_list = (new_list * new_list for new_list in old_list) # yes 1_4_9_16_25_
# new_list = [new_list1 * new_list for new_list in old_list] # NameError: name 'new_list1' is not defined
# new_list = [new_list * new_list for new_list2 in old_list] # NameError: name 'new_list' is not defined
old_list = (old_list * old_list for old_list in old_list) # yes 1_4_9_16_25_
for item in new_list:
print(item, end="_")
print("")
for item in old_list:
print(item, end="_")
print("")
3、集合推導式
# coding:utf-8
# Time:2022/6/28 20:57
# Author:Yang Xiaopeng
"""
語法格式
{表達式 for 變量 in 變量}
{表達式 for 變量 in 變量 if 條件表達式}
上述格式中 的 表達式中的變量與for變量一致
"""
old_list = {1, 2, 3, 4, 5}
new_list = {new_list * new_list for new_list in old_list} # yes {1, 4, 9, 16, 25}
# new_list = {new_list1 * new_list for new_list in old_list} # NameError: name 'new_list1' is not defined
# new_list = {new_list * new_list for new_list2 in old_list} # NameError: name 'new_list' is not defined
old_list = {old_list * old_list for old_list in old_list} # yes {1, 4, 9, 16, 25}
print(old_list)
print(new_list)
new_list = {old_list for old_list in old_list if old_list % 2 == 1} # yes {1, 9, 25}
print(new_list)
4、字典推導式
# coding:utf-8
# Author:Yang Xiaopeng
"""
語法格式
{key : value for key in 變量}
{key : value for key in 變量 if 表達式}
"""
old_dict = ["Zhang", "Wang", "Yang", "Jim"]
new_dict = {key:len(key) for key in old_dict} # yes {1, 4, 9, 16, 25}
print(old_dict)
print(new_dict)
new_dict = {lll:len(lll) for lll in old_dict if len(lll) % 2 == 0} # yes {1, 9, 25}
print(new_dict)
原文鏈接:https://blog.51cto.com/u_12907475/5458642
相關推薦
- 2023-11-22 Linux在Ubuntu Linux下如何壓縮一個文件夾/文件
- 2022-09-25 Linux基礎組件之死鎖檢測
- 2022-04-11 nginx從安裝到配置詳細說明(安裝,安全配置,防盜鏈,動靜分離,配置?HTTPS,性能優化)_ng
- 2022-12-08 python求兩個時間的時間差(實例代碼)_python
- 2022-03-27 NGINX?權限控制文件預覽和下載的實現原理_nginx
- 2022-07-19 Android通過交互實現貝塞爾曲線的繪制_Android
- 2022-07-19 react props的特點
- 2023-01-19 python使用?f?格式化字符串的用法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支