Python(high_level_func)

"""
高阶函数:
map
reduce
sorted
filter
zip
"""

"""
 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(func, *iterables)
参数: func: 函数
      *iterables: 一个或多个可迭代对象
返回值: map object
功能: 制作一个迭代器(可迭代的对象) 通过使用函数,需要传入的参数来自每一个可迭代对象。
      什么时候停止:当最短的可迭代对象被耗尽的时候停止
注意:定义这个函数的时候,需要注意有多少个可迭代对象传入,就有多个参数     
"""
list_data = [1, 2, 3]
list_data1 = [4, 5, 6, 7]
list_data2 = [8, 9, 10, 11, 12]

def computes_func(x, y, z):
    return x + y + z

map_obj = map(computes_func, list_data, list_data1, list_data2)
print(map_obj, type(map_obj))
print(list(map_obj))

# reduce
"""
reduce 迁入到其他模块(functools)了,需要引入
def reduce(function, sequence, initial=_initial_missing):
参数:
   function: 函数
   sequence: 序列
   initial: 初始化,如果存在initial,则在计算中将其置于序列项之前,并在序列为空时用作默认值
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
=》 ((((1+2)+3)+4)+5)
=》 x = 1, y = 2 => 3
=>  x = 3, y = 3 

    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

data = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
print(data, type(data))

"""
sorted: 排序
和sort的区别: list中sort的方法,所以它依赖于list
sorted(iterable, /, *, key=None, reverse=False)
它不依赖于list,所以它排序的返回比较广: 对iterable进行排序
list.sort(/, *, key=None, reverse=False)
只针对列表进行排序
list.sort

但是:两个在排序上是一样的,只是调用的方式不同
list.sort(): 在原列表进行排序
sorted(): 产生一个新的列表
"""
# help(list)

list_data = [5, 2, 7, 1, 9, 3, 4]
sorted(list_data)
list_data.sort()

"""
filter: 过滤
filter(function or None, iterable)
参数: 
function or None: 函数或者空
iterable: 可迭代对象(一个)
返回值:
filter object
功能:过滤=> 肯定有过滤的条件
 
"""
# help(filter)
list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def filter_func(x):
    if x % 2 != 0:
        return True

filter_obj = filter(filter_func, list_data)
print(filter_obj, type(filter_obj))
print(list(filter_obj))


"""
 zip(*iterables) --> 
 A zip object yielding tuples until an input is exhausted.
 参数:*iterable: 一个或多个可迭代对象
 返回值: zip object
 功能:从你传入多个可迭代对象,去从每一个可迭代对象中取元素,
      第一个去取所有可迭代对象的第一个元素
      第二次去取所有可迭代对象的第二个元素
      。。。。。。
      什么时候结束: 最短的被耗尽了
      
三个元组,每个元组有三个元素
"""
# help(zip)



data = zip([1, 2], [4, 5, 6], [7, 8, 9])
print(data)
print(list(data))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值