Python基础学习lesson5

北京市中科医院 https://jbk.39.net/yiyuanzaixian/bjzkbdfyy/jzpj/
Python第五课的基础知识01:59来自LearningYard学苑

01排序与逆序

sorted()对列表、元组、字典、集合或其他可迭代对象排序并返回新列表

reversed()对可迭代对象进行翻转并返回为可迭代的reversed对象

sorted()sortslists,tuples,dictionaries,collectionsorotheriterableobjectsandreturnsanewlist

reversed()reversestheiterableobjectandreturnsitasaniterablereversedobject

例1:

例2:

02枚举与迭代

enumerate()函数用来枚举可迭代对象中的元素,返回可迭代的enumerate对象,其中每个元素都是包含索引和值的元组

Theenumerate()functionisusedtoenumeratetheelementsinaniterableobjectandreturnaniterableenumerateobject,whereeachelementisatuplecontainingindexandvalue

例1:

枚举range对象中的元素:

enumerate对象被list()读取后,就不复存在

03map()、reduce()、filter()、range()、zip()

1.map()把一个函数func映射到序列或迭代器对象的每个元素上,并返回可迭代的map对象作为结果。

1.map()mapsafunctionfunctoeachelementofthesequenceoriteratorobject,andreturnsaniterablemapobjectastheresult.

把单参数函数映射到一个序列的所有元素:

把双参数函数映射到两个序列上:

2.标准库functools中的函数reduce(a,b)将b依次作用在a上

2.Thefunctionreduce(a,b)inthestandardlibraryfunctoolsappliesbtoainturn

3.内置函数filter()将单参数函数作用到序列上,返回该序列中使得该函数返回值为True的那些元素组成的filter对象

如果指定函数为None,则返回序列中等价于True的元素

3.Thebuilt-infunctionfilter()appliesasingle-parameterfunctiontothesequenceandreturnsthefilterobject


转载请注明:http://www.aierlanlan.com/rzgz/1330.html