内置函数

内建函数 built-in

map

map(function, iterable[, ...]) -> list

  • function 是一个函数或者lambda表达式
  • iterable是一个或者多个可以被迭代的序列
  • 返回值是一个map类型的list

Apply function to every item of iterable and return a list of the results. 参数序列中的每个元素分别调用function函数,并且返回 每次function函数返回值得list。

If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. 如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用function。

If one iterable is shorter than another it is assumed to be extended withNoneitems. 如果一个可迭代比另外一个短,将会被拓展为空项。

If function isNone, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). 如果带有多个参数 map返回一个 由元组(从每个可迭代序列中取出一个元素)构成的列表(是一种变换操作,笛卡尔积)

The iterable arguments may be a sequence or any iterable object; the result is always a list. 可迭代参数可以是序列或者其他可迭代的对象;结果始终是一个列表。

将function应用于iterable的每一个元素,返回结果的列表。如果有额外的iterable参数,并行的从这些参数中取元素,并调用function。如果一个参数比另外的要短,将以None扩展该参数元素。如果function是None使用特性函数;如果有多个参数,map()返回一元组列表,元组包含从各个参数中取得的对应的元素(某种变换操作)。iterable参数可以是序列或者任意可迭代对象;结果总是列表。

注意 map()函数不改变原有的 list,而是返回一个新的 list。

map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

在python3中则有所不同

Help on class map in module builtins:

class map(object)
 |  map(func, *iterables) --> map object
 |  
 |  Make an iterator that computes the function using arguments from
 |  each of the iterables.  Stops when the shortest iterable is exhausted.

返回的不再是list 而是一个map对象,需要我们再调用list转换为列表

>>> lst = list(map(lambda x:x**2 ,[1,3,5,7,9]))
>>> lst
[1, 9, 25, 49, 81]
>>> a = [1,2,3]
>>> print (list(map(None,a)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

filter

python2

filter(...)
    filter(function or None, sequence) -> list, tuple, or string

    Return those items of sequence for which function(item) is true.  If function is None, return the items that are true.  If sequence is a tuple or string, return the same type, else return a list.
......

python3

class filter(object)
 |  filter(function or None, iterable) --> filter object
 |
 |  Return an iterator yielding those items of iterable for which function(item)
 |  is true. If function is None, return the items that are true.
......
  • function:接受一个参数,返回布尔值True或False

  • sequence或者iterable:可迭代序列可以是str,tuple,list

同理 filter函数也不会改变原序列的数据。

该函数根据function参数返回的结果是否为真 来决定 是否过滤 参数二 中的项,最后返回一个列表。

以python3为例

lst = list(filter(None,[1,2,3,4])) lst [1, 2, 3, 4]

a = [1, 2, 3, 4, 5] lst = list(filter(lambda x: x,a)) lst [1, 2, 3, 4, 5]

说说为什么是这个效果? (从lambda表达式的特点入手)

reduce

reduce函数即为化简: 每次迭代 将上次的迭代结果与下一项继续计算。

python2

reduce(...)

    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

python3

>>> import functools
>>> help(functools.reduce)
Help on built-in function reduce in module _functools:

reduce(...)
reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. 

    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
  • function:该函数有两个参数
  • sequence:序列可以是str,tuple,list
  • initial:固定初始值

reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。 第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial 作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。 注意function函数不能为None。

from functools import reduce reduce(lambda x,y:x+y, range(100)) 4950

sorted

sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

  • iterable 可迭代的序列
  • cmp 比较功能代码
  • key 排序的键
  • reverse 表示是否降序 False表示升序,True表示降序

返回有序的副本。

>>> a = [1,232154,436,567]
>>> sorted(a)
[1, 436, 567, 232154]
>>> a
[1, 232154, 436, 567]
>>> sorted(a,reverse=False)
[1, 436, 567, 232154]
>>> sorted(a,reverse=True) 
[232154, 567, 436, 1]

>>> stulst = [['zhangsan',29],['lisi',19],['wangwu',99],['ana',55]]
>>> sorted(stulst)
[['ana', 55], ['lisi', 19], ['wangwu', 99], ['zhangsan', 29]]

>>> sorted(stulst,key= lambda x:x[1])
[['lisi', 19], ['zhangsan', 29], ['ana', 55], ['wangwu', 99]]

>>> sorted(stulst,key= lambda x:x[1],reverse=True)
[['wangwu', 99], ['ana', 55], ['zhangsan', 29], ['lisi', 19]]

results matching ""

    No results matching ""