python内建函数(二)序列相关sorted、reversed、range、enumerate

序列(容器):指的是一块可存放多个值的连续内存空间

         包括,字符串、列表、元组、字典、集合等

len(x) 返回序列的长度

max(x) 返回序列的最大值元素

min(x) 返回序列的最小值元素

sum(x) 返回序列中所有元素的和(元素必须是数值类型)

any(x) 真值测试,如果列表中其中一个值为真值则返回True

all(x) 真值测试,如果列表中所有值为真值则返回True

1、len(obj)

  参数:obj,必选,为一个序列(容器)

  作用:求序列的长度,即序列中的元素数量

  返回值:返回序列的长度

2、max()

  找出序列中的最大元素。注意,对序列使用 sum() 函数时,做加和操作的必须都是数字,不能是字符或字符串,否则该函数将抛出异常,因为解释器无法判定是要做连接操作(+ 运算符可以连接两个序列),还是做加和操作。

  参数:不能为空。有一个参数时,参数必须为可迭代对象,则返回序列的最大值。若为多个参数(参数类型要统一,否则会报错)则返回最大参数

  作用:求序列或多个参数的最大值

  返回值:返回序列的最大值,或多个参数的最大值

In [160]: max([1,2,3],12,"asd")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-160-12f6f3666cb7> in <module>
----> 1 max([1,2,3],12,"asd")

TypeError: '>' not supported between instances of 'int' and 'list'

In [161]: max("[1,2,3]","12","asd")
Out[161]: 'asd'

3、min()

  和max()相反

4、sum(iterable, start=0)

  此函数专门用于数值,对数值序列(列表、元组、集合、字典)求和 。字典只针对键而言。字符串不能使用此函数。

  参数:至少要有一个参数,最多两个参数。为一个参数时必须为数值序列(可迭代对象,序列元素只能是数字),对其求和

     为两个参数时,第一个为数值序列,第二个为数字

  返回值:返回数值序列的求和结果

In [173]: sum([1,2,3,4,5],0)
Out[173]: 15

In [174]: sum([1,2,3,4,5],3)
Out[174]: 18

In [175]: sum("12345")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-175-ee9980c8d02f> in <module>
----> 1 sum("12345")

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [176]: sum((1,2,3,4,5))
Out[176]: 15

In [177]: sum({1,2,3,4,5})
Out[177]: 15

In [178]: sum({1:"12",2:"we",3:"sd",4:"sda",5:"dc"})
Out[178]: 15

In [179]: sum((1,2,3,4,5),10)
Out[179]: 25

In [180]: sum((1,2,3,4,5),10,1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-180-415b51c7ecdc> in <module>
----> 1 sum((1,2,3,4,5),10,1)

TypeError: sum expected at most 2 arguments, got 3

5、str()

  字符串构造函数

  可以将序列转换为字符串

  参数可为空,为空时,生成一个空字符串

In [209]: str([1,2,3])
Out[209]: '[1, 2, 3]'

In [210]: str((1,2,3))
Out[210]: '(1, 2, 3)'

In [211]: str({1,2,3})
Out[211]: '{1, 2, 3}'

In [212]: str({1:"dw",2:"sdad",3:"sd"})
Out[212]: "{1: 'dw', 2: 'sdad', 3: 'sd'}"

In [213]: str()
Out[213]: ''

6、list()

  可以将序列转换为列表

  参数可为空,为空时,生成一个空列表 

In [214]: list((1,2,3))
Out[214]: [1, 2, 3]

In [215]: list({1,2,3})
Out[215]: [1, 2, 3]

In [216]: list({1:"dw",2:"sdad",3:"sd"})
Out[216]: [1, 2, 3]

In [217]: list("123")
Out[217]: ['1', '2', '3']

In [218]: list()
Out[218]: []

7、sorted(iterable, key=None, reverse=False) 

  参数:至少一个,iterable。

  作用:对iterable进行排序,默认升序,即默认reverse=False。可以指定排序规则,即根据什么排序,key

  返回值:返回一个新列表,新列表是对于iterable的排序结果

>>> l
[1, 2, 5, 4, 3]
>>> sorted(l)
[1, 2, 3, 4, 5]
>>> l
[1, 2, 5, 4, 3]
>>> s="12765"
>>> sorted(s)
['1', '2', '5', '6', '7']
>>> sorted()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'iterable' (pos 1) not found
>>> 

8、reversed(sequence)

  参数:必选,且必须是序列

  作用:返回反向顺序的序列

  返回值:是一个对象,原来sequence的反向,可用for循环访问。

9、enumerate(iterable,start=0)

 

  参数:iterable,必选,可迭代对象。start,可选,默认为0,start必须为整数 

 

  作用:用可迭代对象iterable生成一个枚举对象。枚举,一一列举

 

  返回值:返回一个enumerate对象(枚举对象)。

 

      枚举对象的每一个元素是一个元组,由 start 和 iterable 的元素组成,start默认从0开始。

      enumerate对象可以用next()取值。  

 

In [43]: s
Out[43]: 'weqert'

In [44]: g=enumerate(s,1)

In [45]: next(g,100)
Out[45]: (1, 'w')

In [46]: next(g,100)
Out[46]: (2, 'e')

In [47]: next(g,100)
Out[47]: (3, 'q')

In [48]: next(g,100)
Out[48]: (4, 'e')

In [49]: next(g,100)
Out[49]: (5, 'r')

In [50]: next(g,100)
Out[50]: (6, 't')

In [51]: next(g,100)
Out[51]: 100

In [52]: next(g,100)
Out[52]: 100

10、range()

  也叫整数序列生成器。生成一个可迭代对象(整数序列)

  参数:至少一个,最多3个

  range(stop) 从零开始,每次生成一个整数后加1操作,直到stop为止(不包含stop)

  range(start, stop[, step]) 从 start开始,每次生成一个整数后移动step,直到stop为止

    (不包含stop,且step可以是负数。step默认是1,。step若为正数,start小于stop。step若为负,start大于stop)

  常和for语句结合使用。

  

>>> for i in range(5,10,-1): #错误
...     print(i)
... 
>>> for i in range(10,6,-1):
...     print(i)
... 
10
9
8
7
>>> for i in range(10,6):#错误
...     print(i)
... 
>>> for i in range(6,10):
...     print(i)
... 
6
7
8
9
>>> 

 

  

posted @ 2020-12-17 20:33  昱成  阅读(401)  评论(0编辑  收藏  举报