python内置函数:map、filter、reduce、偏函数

# author:wsm805@qq.com
# create:2019/9/9 9:48
# reversion:1.0

"""
匿名函数
map
filter
reduce
偏函数
"""


# 匿名函数:没有函数名,只执行一句话
### 几种匿名函数的写法

# f1 = lambda: print("f1")
# print(f1)  # f1相当于函数名的id
# f1()

# f2 = lambda a: print(a)
# f2(2)

# f3 = lambda a: a
# result = f3(3)
# print(result)

# f4 = lambda: 4
# print(f4())

# f5 = lambda a,b,c:print(a,b,c)
# f5(1,2,3)

# f6 = lambda a, b, c: (a, b, c)
# f6(4,5,6)

# # 函数名可以省略,直接调用
# (lambda a: print("hello", a))("tom")
# (lambda: print("hhh"))()
# print((lambda x: x + 1)(2))
# print((lambda x, y: x + y)(1, 2))

# s = "hgjfdkdghfiuawebl"
# s = list(s)
# s.sort(key=lambda x: x.lower())  # key为可迭代对象,是排序的依据
# print(s)

# # map(函数,x)-->映射关系的结果序列
# list1 = [1, 2, 3, 4, 5, 6, 7]
# list2 = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
# print(list(map((lambda x, y: (x, y)), list1, list2)))
# [(1, '一'), (2, '二'), (3, '三'), (4, '四'), (5, '五'), (6, '六'), (7, '七')]

#
# list1 = [0, 1, 2, 3, 4, 5]
# print(list(map((lambda x: x + 1), list1)))

# # filter过滤出函数中结果为True的值
# list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# print(list(filter(lambda x: x if x % 2 == 0 else None, list1)))
# print(list(map(lambda x: x if x % 2 == 0 else None, list1)))
# for i in filter(lambda x: x % 2 == 0, list1):
#     print(i)

# # reduce 累计,返回值为累计结果
# from _functools import reduce
#
# list1 = [1, 2, 3, 4, 5, 6]
# print(reduce(lambda x, y: x + y, list1))

# # 偏函数,将一个函数的某些参数固定住(设置默认值),以减少后续调用函数时重复输入相同的参数
#
# # _functools和functools的区别
# import _functools
# import functools
# print(dir(_functools))
# print(dir(functools))
# # ['__doc__', '__loader__', '__name__', '__package__', '__spec__', '_lru_cache_wrapper', 'cmp_to_key', 'partial', 'reduce']
# # ['MappingProxyType', 'RLock', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', 'WeakKeyDictionary', '_CacheInfo', '_HashedSeq', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_c3_merge', '_c3_mro', '_compose_mro', '_convert', '_find_impl', '_ge_from_gt', '_ge_from_le', '_ge_from_lt', '_gt_from_ge', '_gt_from_le', '_gt_from_lt', '_le_from_ge', '_le_from_gt', '_le_from_lt', '_lru_cache_wrapper', '_lt_from_ge', '_lt_from_gt', '_lt_from_le', '_make_key', 'cmp_to_key', 'get_cache_token', 'lru_cache', 'namedtuple', 'partial', 'partialmethod', 'recursive_repr', 'reduce', 'singledispatch', 'total_ordering', 'update_wrapper', 'wraps']
# 函数使用方法相同,带‘_’是受保护的模块
#
# import functools
#
# def func1(a, b, c, d):
#     print(a, b, c, d)
#
# par = functools.partial(func1, "参数1", "参数2")
# par("c1", "d1")
# par("c2", "d2")
# par("c3", "d3")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值