Python3 部分内建高阶函数

1、map()

  • Map() 是一种内置的 Python 函数,它可以将函数应用于各种数据结构中的元素,如列表或字典。
  • 对于这种运算来说,这是一种非常干净而且可读的执行方式。
def square_it_func(a):
    return a * a

x = map(square_it_func, [1, 4, 7])
print(x)
print(list(x))

def multiplier_func(a, b):
    return a * b

x = map(multiplier_func, [1, 4, 7], [2, 5, 8])
print(x)
print(list(x))
<map object at 0x00000000074C4048>
[1, 16, 49]
<map object at 0x00000000074C4A88>
[2, 20, 56]

2、filter()

Init signature: filter(self, /, *args, **kwargs)
Docstring:     
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 内置函数与 map 函数非常相似,它也将函数应用于序列结构(列表、元组、字典)。
  • 二者的关键区别在于 filter()将只返回应用函数返回 True 的元素。
# Our numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# Function that filters out all numbers which are odd
def filter_odd_numbers(num):
    return True if num % 2 == 0 else False

filtered_numbers = filter(filter_odd_numbers, numbers)

print(filtered_numbers)
print(list(filtered_numbers))
<filter object at 0x00000000074B8C88>
[2, 4, 6, 8, 10, 12, 14]
  • 对可迭代对象进行遍历,返回一个迭代器
  • function 参数是一个参数的函数,且返回值应当是 bool 类型,或其返回值等效于 bool 值
  • function 参数如果是 None,可迭代对象的每一个元素自身等效 bool 值
list(filter(None, range(-5, 5)))
# [-5, -4, -3, -2, -1, 1, 2, 3, 4]
list(filter(None, [1, 0, [], {}, (0, ), None, False, True]))
# [1, (0,), True]
list(filter(lambda x: x%3 ==0 and x%2 == 0, range(29)))
# [0, 6, 12, 18, 24]
list1 = ['asv', 'avv', 'rrr']
print(list(filter(lambda x:x.startswith('a'), list1)))
# ['asv', 'av']

3、sorted

Signature: sorted(iterable, /, *, key=None, reverse=False)
Docstring:
Return a new list containing all items from the iterable in ascending order.

A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
nums = list(range(6))
print(1, nums)
print(2, sorted(nums, key=int))
print(3, sorted(nums, key=lambda x: 6-x))
print(4, sorted(nums, key=str, reverse=True))
print(5, nums)  # 对原对象没有影响
1 [0, 1, 2, 3, 4, 5]
2 [0, 1, 2, 3, 4, 5]
3 [5, 4, 3, 2, 1, 0]
4 [5, 4, 3, 2, 1, 0]
5 [0, 1, 2, 3, 4, 5]

4、sorted 与 list.sort 区别

Signature: list.sort(self, /, *, key=None, reverse=False)
Docstring: Stable sort *IN PLACE*.
Type:      method_descriptor
nums = list(range(6))
print(nums)
print(nums.sort(key=lambda x: 6-x))
print(nums)
[0, 1, 2, 3, 4, 5]
None
[5, 4, 3, 2, 1, 0]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程概述教会学员快速学会python数据分析,覆盖python基础,pandas,seaborn,matplotlib,SQL,sqlite,lambda等知识。课程是数据科学家居家必备的军火库。课程定期更新,大部分视频分辨率支持2K超清,学员可以看清每一行代码。 适合人群python数据科学从业人员,数据分析师,统计 学习计划和方法1.每天保证1-2个小时学习时间,预计7-15天左右可以学习完整门课程(不同基础学生时间差异较大)。2.每节课的代码实操要保证,议不要直接复制粘贴代码,自己实操一遍代码对大脑记忆很重要,有利于巩固知识。3.第二次学习时要总结上一节课内容,必要时做好笔记,加深大脑理解。4.不懂问题要罗列出来,先自己上网查询,查不到的可以咨询老师。 作者介绍Toby,持牌照金融公司担任模型验证专家,国内最大医药数据中心数据挖掘部门负责人!和清华大学出版社,重庆儿科医院,中科院教授,赛柏蓝保持慢病数据挖掘项目合作!管理过欧美日中印巴西等国外药典数据库,马丁代尔数据库,FDA溶解度数据库,临床试验数据库,WHO药物预警等数据库。原创公众号(python风控模型) 课程概述教会学员快速学会python数据分析,覆盖python基础,pandas,seaborn,matplotlib,SQL,sqlite,lambda等知识。课程是数据科学家居家必备的军火库。课程定期更新,大部分视频分辨率支持2K超清,学员可以看清每一行代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值