網站首頁 編程語言 正文
前言
推導式是一種獨特的數據處理方式,可以快速的從一個數據序列構建另一個新的數據序列的結構體。常用的推導式有一下四種:
- 列表推導式
- 元組推導式
- 集合推導式
- 字典推導式
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
相關推薦
- 2022-11-16 docker-compose安裝及執(zhí)行命令_docker
- 2022-12-23 C++成員函數如何當作回調函數同時傳遞this指針_C 語言
- 2022-05-19 聊聊Python代碼中if?__name__?==?‘__main__‘的作用是什么_python
- 2022-03-20 Maven工程pom中如何定義jdk版本(maven配置jdk版本)
- 2022-01-27 laravel JWTAuth對api接口權限進行鑒權
- 2023-04-29 vscode搭建python?Django網站開發(fā)環(huán)境的示例_python
- 2023-08-28 Antd的日期選擇器中文化配置
- 2022-10-29 subplots_adjust()函數--matplotlib
- 最近更新
-
- 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)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支