python内置函数

1.map()

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.

map函数返回一个迭代器,迭代器的结果为iterable的每个元素在func函数中运算的结果。如果有多个迭代对象,只按最短的计算。
注意:只有当func函数本身支持多个参数输入时才可以输入对应个数iterable;*输出的是运算结果,不是iterable本身下例:

print(list(map(lambda x, y: x+y, ['liu_sb', 'wang_sb', 'li_sb'],
               ['liu_bitch', 'wang_bitch'])))
#输出
['liu_sbliu_bitch', 'wang_sbwang_bitch']

2.filter()

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.

filter函数返回一个迭代器,迭代器的结果为iterable的每个元素在func函数中运算结果True的iterable元素。如果函数运算结果为None,返回元素本身布尔运算结果为True 的元素。
注意:返回的是iterable元素,不是运算结果。

print(list(filter(lambda x: x.endswith('sb'), ['liu_sb', 'wang_', 'li_sb'])))

print(list(filter(lambda x: x+'sb', ['liu_sb', 'wang_', 'li_sb'])))

print(list(filter(None, ['liu_sb', 'wang_', 'li_sb', 0])))
#result
['liu_sb', 'li_sb']
['liu_sb', 'wang_', 'li_sb']
['liu_sb', 'wang_', 'li_sb']

3.reduce()
python3中reduce需从functools模块导入

functools.reduce(func, iter, [initial_value]) cumulatively performs an operation on all the iterable’s elements and, therefore, can’t be applied to infinite iterables. func must be a function that takes two elements and returns a single value. functools.reduce() takes the first two elements A and B returned by the iterator and calculates func(A, B). It then requests the third element, C, calculates func(func(A, B), C), combines this result with the fourth element returned, and continues until the iterable is exhausted. If the iterable returns no values at all, a TypeError exception is raised. If the initial value is supplied, it’s used as a starting point and func(initial_value, A) is the first calculation.

reduce函数从iterable中取出2个元素进行运算(如果给出initial value值,将只取一个元素)将本次运算的值作为下次的参数再输入函数,并继续从iterable中取出一个元素一起运算,如此一直类计循环直到元素取尽为止,最后返回最终运算结果。如iterable是空的,直接报错。
注意:1.函数有且只能有两个必选参数(可以有默认参数和可选参数);
2.返回值只能有一个(事实上就算有多个返回值,返回的也是一个元组,只要元组能作为函数参数继续参与运算也可以)
3.如果iterable是有顺序的,那按顺序从第一个元素取;
4.某些情况下可以替代for循环使用;

示例:

from functools import reduce

def ab(x,y, z=6):
    return  [y*2], [x*2], [z]


print(reduce(ab, [1,2,3,4], 1))
#result
([8], [([6], [([4], [([2], [2], [6], [2], [2], [6])], [6], [4], [([2], [2], [6], [2], [2], [6])], [6])], [6], [6], [([4], [([2], [2], [6], [2], [2], [6])], [6], [4], [([2], [2], [6], [2], [2], [6])], [6])], [6])], [6])

4.zip()

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.

zip函数通过输入一组序列,来返回一个zip对象,通过.next()方法会返回一个元组:将每个序列的第i个元素一一对应。

s=zip([1,2,3], [4,6,8])
print(list(s))
#result    [(1, 4), (2, 6), (3, 8)]

zip函数也可用于字典:如zip(dict.values(),dict.keys())
5.max()

max(iterable, *[, key, default])max(arg1, arg2, *args[, key])
Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.
If multiple items are maximal, the function returns the first one encountered.

max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.

man函数有两种使用模式,一种是单iterable,返回iterable中的最大元素,并有可选参数default=(用来指定iterable为空时的返回对象) 和key=(用来对iterable的每个元素进行运算,运算后再进行比较);另一种为多参数形式,返回最大参数,可选参数只有key没有default.

max([3,4,5],[2,3,8])
[3, 4, 5]
max(zip([1,2,3], [4,6,8]))
(3, 8)
max([], default='shit')
'shit'
max('string','fuck','shit', key=str.upper)
'string'

注意:man()返回的依然是原输入参数之一或iterable的元素之一,并不是key计算后的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值