可通过help(xxx)来查看x x x函数的相关信息,如下图
如果有__iter__()说明该内建函数可以迭代器。
- 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.
>>> a= [1,2,3,4,5,6,7]
>>> list(filter(lambda x:x>2,a))
[3, 4, 5, 6, 7]
- 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.
>>> a = [1,2,3]
>>> map(lambda x:x,a)
<map object at 0x10d218828>
>>> list(map(lambda x:x,a))
[1, 2, 3]
>>> list(map(lambda x:x+1,a))
[2, 3, 4]
>>> b= [4,5,6]
>>> list(map(lambda x,y:x+y,a,b))
[5, 7, 9]
- 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.
# 导入才可使用reduce
>>> from functools import reduce
# sum = ((1+2)+3)+4
>>> reduce(lambda x,y:x+y,[2,3,4],1)
10
- zip(iter1 [,iter2 […]]) --> zip object
Return a zip object whose .next() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The .next()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
4.1 将元组转置
>>> zip((1,2,3),(4,5,6))
<zip object at 0x10734d208>
>>> list(zip((1,2,3),(4,5,6)))
[(1, 4), (2, 5), (3, 6)]
>>> for i in zip((1,2,3),(4,5,6)):
... print(i)
...
(1, 4)
(2, 5)
(3, 6)
>>>
4.2 对调dict 的value和key
>>> dict1={'a':'aaa','b':'bb'}
>>> zip(dict1.values(),dict1.keys())
<zip object at 0x10734d508>
>>> dict2=zip(dict1.values(),dict1.keys())
>>> print(dict(dict2))
{'aaa': 'a', 'bb': 'b'}
>>>