您的位置:首页 > 脚本大全 > > 正文

python函数参数讲解(Python高级特性与几种函数的讲解)

更多 时间:2021-11-08 16:29:42 类别:脚本大全 浏览量:1758

python函数参数讲解

Python高级特性与几种函数的讲解

切片

从list或tuple中取部分元素。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • list = [1, 2, 3, 4]
  • list[0 : 3] # [1, 2, 3]
  • list[-2 : -1] # -1表示最后一个,[3, 4]
  • list[1 :: 2] # index = 1开始,每两个取一个[2, 4]
  • list[:] # 复制list,[1, 2, 3, 4]
  • # 针对tuple,切片同样适用
  • iterable、iterator

    可迭代,迭代器,集合类型数据可迭代但不是迭代器,可通过iter()转变为迭代器。

    可迭代对象可使用for-in语句遍历,判断x是否可迭代:isinstance(x, Iterable)。

    列表生产式

    高效创建列表,见代码示例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • # range转list
  • list(range(1, 5)) # [1, 2, 3, 4]
  • [x * x for x in range(1, 5)] # [1, 4, 9, 16]
  • [x * x for x in range(1, 5) if x % 2 == 0] # [4, 16]
  • [m + n for m in 'ABC' for n in 'XYZ'] # ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
  • [s.lower() for s in ['Hello', 'World', 'IBM', 'Apple']] # like map
  • generator

    isinstance(generator, Iterable) = True,可使用for-in语句,或者使用next方法。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • g = (x * x for x in range(10))
  • next(g) # 0
  • next(g) # 1
  • next(g) # 4
  • for item in g:
  •   print(item) # 9 16 ... 81
  • generator函数

    generator函数本质是一个有状态的函数,遇到yield语句时会暂时返回。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • # 有yield语句,表明时generator函数
  • def gen_fn():
  •   init = 0
  •   while init < 10:
  •     yield init
  •     init += 1
  •   return 'done'
  • call = gen_fn() # 获得可迭代对象call
  • next(call) # 0
  • next(call) # 1
  • # 每次调用generator函数,得到的是一个新的generator
  • # for-in无法获得generator的返回值'done'
  • for item in gen_fn():
  •   print(item) # 0 1 ... 9
  • 高阶函数

    参数是函数的函数即是高阶函数,可对比数学概念:g(x) = f(x) + 1,g(x)即高阶函数。

    • map
  • ?
  • 1
  • 2
  • 3
  • # map(func, *iterables, ...)
  • i = map(lambda x : x * x, [1, 2, 3]) # 返回Iterator
  • list(i) # [1, 4, 9]
    • reduce
  • ?
  • 1
  • 2
  • from functools import reduce
  • reduce(lambda previous, x : previous + x, [1, 2, 3, 4]) # 10
    • filter
  • ?
  • 1
  • 2
  • i = filter(lambda x : x % 2 == True, [1, 2, 3, 4])
  • list(i) # [1, 3]
    • sorted 默认升序,通过key参数决定排序规则。
  • ?
  • 1
  • sorted([1,3,2], key = lambda x : -x) # [3, 2, 1]
  • 返回函数做回函数返回值

    闭包概念:包含环境成分(自由变量)和控制成分的实体(lambda表达式,函数)。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • def lazy_sum(*args):
  •   ax = 0
  •   def sum():
  •     nonlocal ax
  •     for n in args:
  •       ax = ax + n
  •     return ax
  •   return sum
  • fn = lazy_sum(1, 2, 3# ax + sum构成了闭包
  • fn() # 6
  • fn() # 12
  • 匿名函数

    即lambda表达式。

    装饰器

    函数包函数的语法糖?

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • def log(fn):
  •   def call(*args, **kw):
  •     print('call %s():' % fn.__name__)
  •     return fn(*args, **kw)
  •   return call
  • # @log的作用等同now = log(now)
  • @log
  • def now():
  •   print('2018-03-18')
  • now() # call now(): 2018-03-18
  • 偏函数

    把一个函数的某些参数给固定住,返回一个新的函数。类似柯里化,但更强大?

  • ?
  • 1
  • 2
  • 3
  • from functools import partial
  • binary_int = partial(int, base = 2)
  • binary_int('1000000') # 64
  • 总结

    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接

    原文链接:https://amsimple.com/blog/article/38.html

    您可能感兴趣