温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python推导式数据处理方式是什么

发布时间:2022-07-12 14:08:26 来源:亿速云 阅读:116 作者:iii 栏目:开发技术

今天小编给大家分享一下Python推导式数据处理方式是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

前言

推导式是一种独特的数据处理方式,可以快速的从一个数据序列构建另一个新的数据序列的结构体。常用的推导式有一下四种:

  • 列表推导式

  • 元组推导式

  • 集合推导式

  • 字典推导式

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)

Python推导式数据处理方式是什么

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("")

Python推导式数据处理方式是什么

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)

Python推导式数据处理方式是什么

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)

Python推导式数据处理方式是什么

以上就是“Python推导式数据处理方式是什么”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI