Python学习笔记 - filter,map,reduce,zip

1. filter

filter函数用于过滤可迭代对象。它的语法是这样的:

filter(function or None, iterable) --> filter object
复制代码

第一个参数是一个函数,第二个参数是一个可迭代对象。filter函数会根据第一个参数返回True还是False来决定第二个参数中每一项元素是否保留,它返回的是一个filter对象。

例子:

filter对象是一个迭代器,它由惰性运算的特性。如果想要得到一个list类型的函数,的借助list函数

2. map

map函数的语法是:

map(func, *iterables) --> map object
复制代码

我理解的它的作用就是,遍历一个(多个)可迭代对象,然后对这个可迭代对象中的每一个元素都执行传入的函数,并把结果作为最终返回的map对象中的元素。

例子:

list_1 = [1, 2, 3, 4, 5]
list_2 = [9, 8, 7, 6, 5]

new_list_1 = map(lambda i: i**2, list_1)
new_list_2 = map(lambda x, y: x + y, list_1, list_2)

print(list(new_list_1))  # [1, 4, 9, 16, 25]
print(list(new_list_2))  # [10, 10, 10, 10, 10]
复制代码

从例子中可以看出map是可以接受多个可迭代对象的。

3. reduce

reduce 不能直接使用,必须从functools模块中导入(python3中):

from functools import reduce

help(reduce)
复制代码

它的语法是这样的:

reduce(function, sequence[, initial]) -> value
复制代码

它接受三个参数,第一个是一个函数,第二个是一个可迭代对象,第三个是个可选参数,初始值,reduce的作用直接看官方解释,我有点不知道怎么说:

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.
复制代码

直接看例子:

from functools import reduce

nums = reduce(lambda x, y: x + y, [1,2,3,4,5])
print(nums)
'''
reduce 计算的过程可以理解为下面这样:
(((1 + 2) + 3) + 4) + 5
'''

nums = reduce(lambda x, y: x - y, [1,2,3,4,5])
print(nums)

'''
reduce 计算的过程可以理解为下面这样:
(((1 - 2) - 3) - 4) - 5
'''
复制代码

如果加上initial参数就是这样:

from functools import reduce

nums = reduce(lambda x, y: x + y, [1,2,3,4,5], 1)
print(nums)
'''
reduce 计算的过程可以理解为下面这样:
((((1 + 1) + 2) + 3) + 4) + 5
'''

nums = reduce(lambda x, y: x - y, [1,2,3,4,5], 1)
print(nums)

'''
reduce 计算的过程可以理解为下面这样:
((((1 -1) - 2) - 3) - 4) - 5
'''
复制代码

4. zip

zip函数的语法是这样的:

zip(iter1 [,iter2 [...]]) --> zip object
复制代码

作用直接看例子:

zip_example = zip((1,2,3), (3,2,1))
print(list(zip_example)) # [(1, 3), (2, 2), (3, 1)]
复制代码

zip将两个元组(只要是可迭代对象就可以)合并了,合并对应关系是:

(1, 2, 3)
 ↓  ↓  ↓
(3, 2, 1)
    ↓↓
(1, 3), (2, 2), (3, 1)
复制代码

注意zip返回的是zip对象,也是一个迭代器,如果想要列表类型,还是得用list函数进行转换。

使用zip可以轻松的将字典的key和value对换:

dict_a = {
    'a': 'aaa',
    'b': 'bbb',
    'c': 'ccc'
}
zip_example = zip(dict_a.values(), dict_a.keys())
print(dict(zip_example))  # {'aaa': 'a', 'bbb': 'b', 'ccc': 'c'}
复制代码

还有一个用法就是用*

zip_example = list(zip((1,2,3), (3,2,1), (3,2,1))) 
print(zip_example) # [(1, 3, 3), (2, 2, 2), (3, 1, 1)]

zip_example_restore = list(zip(*zip_example))
print(zip_example_restore) # [(1, 2, 3), (3, 2, 1), (3, 2, 1)]
复制代码

给zip的参数加上*之后就好像是zip逆过程一样。

转载于:https://juejin.im/post/5c330b51e51d4551e325d635

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值