美文网首页
Python:5.高阶函数

Python:5.高阶函数

作者: 许瘦子来世 | 来源:发表于2018-07-10 15:20 被阅读8次

map && reduce

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# map
'''
1. map()函数接收两个参数,一个是函数,一个是Iterable
2. map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回
'''

def a(x):
    return x * x

b = map(a,[1,2,3,4,5,6,7,8,9])
print(list(b)) # 使用list()函数将整个序列计算并返回一个list

# 把所有数字转换为字符串
print(list(map(str, [1,2,3,4,5,6,7,8,9])))

# reduce
'''
1. 把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数
2. reduce把结果继续和序列的下一个元素做累积计算
3. reduce(f, [x1,x2,x3,x4]) = f(f(f(x1,x2), x3) x4)
'''
from functools import reduce
def add(x, y):
    return x + y

print(reduce(add, [1,3,5,7,9]))

# 把str转换成int函数
'''
def fn(x,y):
    return x * 10 + y

def char2num(s):
    digits = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
    return digits[s]
print(reduce(fn, map(char2num, '13579')))
'''

# 优化方式1
'''
DIGITS = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
def str2num(s) :
    def fn(x, y) :
        return x * 10 + y
    def char2num(s) :
        return DIGITS[s]
    return reduce(fn, map(char2num, s))

print(str2num('13579'))
'''

# 进一步优化
DIGITS = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
def char2num(s):
    return DIGITS[s]

def str2num(s):
    reduce(lambda x, y: x * 10 + y, map(char2num, s))
    
print(str2num('13579'))

filter

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# filter()函数:用于过滤序列
'''
1. 接收一个函数和一个序列
2. 把传入的函数依次作用于每个元素,然后根据返回值是True或False决定保留还是丢弃该元素
3. 结果返回迭代器,Iterator
'''

# 删掉偶数,保留基数
def is_odd(n):
    return n % 2 == 1
print(list(filter(is_odd, [1,2,4,5,6,9,10,15])))

# 用filter()求素数
def _odd_iter():
    n = 1
    while True:
        n = n + 2
        yield n

def _not_divisible(n):
    return lambda x : x % n > 0

def primes():
    yield 2
    it = _odd_iter() # 初始序列
    while True:
        n = next(it) # 返回序列的第一个数
        yield n
        it = filter(_not_divisible, it) # 构造新序列

# 打印1000以内的素数
for n in primes():
    if n < 1000:
        print(n)
    else:
        break

sorted

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 排序算法

print(sorted([23,2,34,56,1,-20]))

# 接收一个key函数来实现自定义排序.
'''
key指定的函数将作用域list的每一个元素上,并根据key函数返回的结果进行排序。
'''
print(sorted([36,-5,-12,9,-21],key=abs))

# 忽略大小写排序
print(sorted(['Bob','tom','james','Credit'],key=str.lower))

# 反向排序,传入第三个参数reverse=True
print(sorted(['Bob','tom','james','Credit'],key=str.lower,reverse=True))

相关文章

  • Python:5.高阶函数

    map && reduce filter sorted

  • python装饰器

    装饰器简述 要理解装饰器需要知道Python高阶函数和python闭包,Python高阶函数可以接受函数作为参数,...

  • Python高阶函数

    本文要点 1.什么是高阶函数 2.python中有哪些常用的高阶函数 什么是高阶函数? 在了解什么是高阶函数之前,...

  • wangyanhua--python2

    基本函数的使用 匿名函数 常用系统高阶函数 高阶函数 常用系统高阶函数 Python递归 安装第三方库 三国小说人...

  • Python函数式介绍一 - 高阶函数

    Python函数式介绍一 - 高阶函数Python函数式介绍二 - 链式调用 最近为了给朋友推广Python函数式...

  • Python高阶函数学习笔记

    python中的高阶函数是指能够接收函数作为参数的函数 python中map()函数map()是 Python 内...

  • 【Python】-014-函数-函数式编程-2

    python内置高阶函数 Filter函数filter(function, sequence) -> list, ...

  • 函数式编程--高阶函数

    高阶函数英文叫Higher-order function。什么是高阶函数?举例说明。变量可以指向函数以Python...

  • 高阶函数(Python)

    什么是高阶函数(Python)? 高阶函数:能接收函数做参数的函数 变量可以指向函数 函数的参数可以接受变量 一个...

  • filter函数有点意思

    filter函数 filter(function, iterable)filter函数是python中的高阶函数,...

网友评论

      本文标题:Python:5.高阶函数

      本文链接:https://www.haomeiwen.com/subject/mewfpftx.html