Python推导式数据处理方式是什么(python,开发技术)

时间:2024-04-30 08:33:55 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

前言

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

  • 列表推导式

  • 元组推导式

  • 集合推导式

  • 字典推导式

1、列表推导式

#coding:utf-8#Author:YangXiaopeng"""语法格式[表达式for变量in变量][表达式for变量in变量if条件表达式]上述格式中的表达式中的变量与for变量一致"""old_list=[1,2,3,4,5]new_list=[new_list*new_listfornew_listinold_list]#yes[1,4,9,16,25]#new_list=[new_list1*new_listfornew_listinold_list]#NameError:name'new_list1'isnotdefined#new_list=[new_list*new_listfornew_list2inold_list]#NameError:name'new_list'isnotdefinedold_list=[old_list*old_listforold_listinold_list]#yes[1,4,9,16,25]print(old_list)print(new_list)new_list=[old_listforold_listinold_listifold_list%2==1]#yes[1,9,25]print(new_list)

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

2、元组推导式

#coding:utf-8#Author:YangXiaopeng"""语法格式(表达式for变量in变量)(表达式for变量in变量if条件表达式)上述格式中的表达式中的变量与for变量一致"""old_list=(1,2,3,4,5)new_list=(new_list*new_listfornew_listinold_list)#yes1_4_9_16_25_#new_list=[new_list1*new_listfornew_listinold_list]#NameError:name'new_list1'isnotdefined#new_list=[new_list*new_listfornew_list2inold_list]#NameError:name'new_list'isnotdefinedold_list=(old_list*old_listforold_listinold_list)#yes1_4_9_16_25_foriteminnew_list:print(item,end="_")print("")foriteminold_list:print(item,end="_")print("")

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

3、集合推导式

#coding:utf-8#Time:2022/6/2820:57#Author:YangXiaopeng"""语法格式{表达式for变量in变量}{表达式for变量in变量if条件表达式}上述格式中的表达式中的变量与for变量一致"""old_list={1,2,3,4,5}new_list={new_list*new_listfornew_listinold_list}#yes{1,4,9,16,25}#new_list={new_list1*new_listfornew_listinold_list}#NameError:name'new_list1'isnotdefined#new_list={new_list*new_listfornew_list2inold_list}#NameError:name'new_list'isnotdefinedold_list={old_list*old_listforold_listinold_list}#yes{1,4,9,16,25}print(old_list)print(new_list)new_list={old_listforold_listinold_listifold_list%2==1}#yes{1,9,25}print(new_list)

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

4、字典推导式

#coding:utf-8#Author:YangXiaopeng"""语法格式{key:valueforkeyin变量}{key:valueforkeyin变量if表达式}"""old_dict=["Zhang","Wang","Yang","Jim"]new_dict={key:len(key)forkeyinold_dict}#yes{1,4,9,16,25}print(old_dict)print(new_dict)new_dict={lll:len(lll)forlllinold_dictiflen(lll)%2==0}#yes{1,9,25}print(new_dict)

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python推导式数据处理方式是什么的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Vue如何实现记住账号密码功能下一篇:

8 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18