Python build-in functions - List

Catalogue
  1. 1. map()
    1. 1.1. overview
    2. 1.2. explain
    3. 1.3. example
  2. 2. reduce()
    1. 2.1. overview
    2. 2.2. explain
    3. 2.3. example
  3. 3. filter()
    1. 3.1. overview
    2. 3.2. explain
    3. 3.3. example
  4. 4. sorted
    1. 4.1. overview
    2. 4.2. explain
    3. 4.3. example

map()

overview

Query or sets dynamic values of the specified option(s) in style.

Each key in kw is an option and each value should be a list or a tuple (usually) containing statespecs grouped in tuples, lists, or something else of your preference. A statespec is a compound of one or more states and then a value.

explain

map()方法接受两个参数,一个是函数,一个是序列list,map将传入的函数依次作用到序列的每个元素,然后返回新的list。

example

def f(x):
    return x * x
map(f, [1, 2, 3, 4])
[1, 4, 9, 16]

reduce()

overview

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.

explain

reduce()将一个函数作用于一个序列list上,必须接收两个参数,reduce将上一个操作的结果作为第一个参数与序列下一个参数参与运算。

example

def add(x, y):
    return x + y
print reduce(add, [1, 2, 3])
6

filter()

overview

Returns an instance of the Filter class. If name is specified, it names a logger which, together with its children, will have its events allowed through the filter. If name is the empty string, allows every event.

explain

filter()接收一个函数和一个序列List,filter依次将传入函数依次作用于序列List中的每一个元素,并根据函数返回值TrueFalse判定是否需要保留该元素。该方法返回一个新序列List。

example

# 利用filter,保留序列List中偶数
def is_odd(x):
    if x % 2 == 0:
        return True
    else:
    return False
filter(is_odd, [1, 2, 3, 4, 5])
[2, 4]

# 利用filter,删除素数
def is_prime(x):
    for i in xrange(2, x):
        if x % i == 0:
            return False
    return True
filter(is_prime, xrange(100))

sorted

overview

Return a new sorted list from the items in iterable.
The optional arguments cmp, key, and reverse have the same meaning as those for the list.sort() method (described in section Mutable Sequence Types).

cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.

key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).

reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

explain

sorted函数接收一个比较函数实现自定义排序。

example

sorted([5, 9, 1, 3])
[1, 3, 5, 9]

def reversed_cmp(x, y):
    if x > y:
        return -1
    if x < y:
        return 1
    return 0


sorted([5, 9, 1, 3], reversed_cmp)
[9, 5, 3, 1]