• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-08) 67次浏览 已收录 0个评论

今天小编就为大家分享一篇关于Python高级特性与几种函数的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

切片

从list或tuple中取部分元素。

 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)。

列表生产式

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

 # 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方法。

 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语句时会暂时返回。

 # 有yield语句,表明时generator函数 def gen_fn(): init = 0 while init <10: yield init +=1 return 'done' call=gen_fn() # 获得可迭代对象call next(call) 0 1 每次调用generator函数,得到的是一个新的generator for-in无法获得generator的返回值'done' for item in gen_fn(): print(item) ... 9<pre></div><p><strong>高阶函数</strong></p><p>参数是函数的函数即是高阶函数,可对比数学概念:<code>g(x) = f(x) + 1</code>,g(x)即高阶函数。</p><ul><li>map</li></ul><div class="gaodaimacode"><pre class="prettyprint linenums"> # map(func, *iterables, ...) i = map(lambda x : x * x, [1, 2, 3]) # 返回Iterator list(i) # [1, 4, 9]
  • reduce
 from functools import reduce reduce(lambda previous, x : previous + x, [1, 2, 3, 4]) # 10
  • filter
 i = filter(lambda x : x % 2 == True, [1, 2, 3, 4]) list(i) # [1, 3]
  • sorted 默认升序,通过key参数决定排序规则。
 sorted([1,3,2], key = lambda x : -x) # [3, 2, 1]

返回函数做回函数返回值

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

 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表达式。

装饰器

函数包函数的语法糖?

 def log(fn): def call(*args, **kw): print('call %s():' % fn.<a style="color:transparent">来源gao($daima.com搞@代@#码网</a>__name__) return fn(*args, **kw) return call # @log的作用等同now = log(now) @log def now(): print('2018-03-18') now() # call now(): 2018-03-18

偏函数

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

 from functools import partial binary_int = partial(int, base = 2) binary_int('1000000') # 64

总结

以上就是Python高级特性与几种函数的讲解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python高级特性与几种函数的讲解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址