Python的高级特征你知多少?来对比瞧瞧
<div style="color: black; text-align: left; margin-bottom: 10px;">Python 多好用<span style="color: black;">不消</span>多说,<span style="color: black;">大众</span><span style="color: black;">瞧瞧</span>自己用的语言就<span style="color: black;">晓得</span>了。<span style="color: black;">然则</span> Python <span style="color: black;">隐匿</span>的高级功能你都 get 了吗?本文中,作者列举了 Python 中五种略高级的特征以及它们的<span style="color: black;">运用</span><span style="color: black;">办法</span>,快来一探<span style="color: black;">到底</span>吧!<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/cc3bc0f1b27e4e9c8864d978da421330~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723892294&x-signature=YfoDBjeUjbZF1ArbtAyfQvQ9FDM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 是一种<span style="color: black;">漂亮</span>的语言,它简单易用却非常强大。但你真的会用 Python 的所有功能吗?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">任何编程语言的高级特征<span style="color: black;">一般</span>都是<span style="color: black;">经过</span><span style="color: black;">海量</span>的<span style="color: black;">运用</span>经验才<span style="color: black;">发掘</span>的。<span style="color: black;">例如</span>你在编写一个<span style="color: black;">繁杂</span>的项目,并在 stackoverflow 上寻找某个问题的答案。<span style="color: black;">而后</span>你<span style="color: black;">忽然</span><span style="color: black;">发掘</span>了一个非常优雅的<span style="color: black;">处理</span><span style="color: black;">方法</span>,它<span style="color: black;">运用</span>了你从不<span style="color: black;">晓得</span>的 Python 功能!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这种学习方式太有趣了:<span style="color: black;">经过</span>探索,偶然<span style="color: black;">发掘</span>什么。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面是 Python 的 5 种高级特征,以及它们的用法。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Lambda 函数</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Lambda 函数是一种比较小的匿名函数——匿名<span style="color: black;">指的是</span>它<span style="color: black;">实质</span>上<span style="color: black;">无</span>函数名。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 函数<span style="color: black;">一般</span><span style="color: black;">运用</span> def a_function_name() 样式来定义,但<span style="color: black;">针对</span> lambda 函数,<span style="color: black;">咱们</span><span style="color: black;">基本</span>没为它命名。这是<span style="color: black;">由于</span> lambda 函数的功能是执行某种简单的表达式或运算,而无需完全定义函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">lambda 函数<span style="color: black;">能够</span><span style="color: black;">运用</span>任意数量的参数,但表达式只能有一个。</p>x = lambda a, b : a * b
print(x(5, 6)) # prints 30
x = lambda a : a*3 + 3
print(x(3)) # prints 12<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">看它多么简单!<span style="color: black;">咱们</span>执行了<span style="color: black;">有些</span>简单的数学运算,而无需定义<span style="color: black;">全部</span>函数。这是 Python 的众多特征之一,这些特征使它<span style="color: black;">作为</span>一种干净、简单的编程语言。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Map 函数</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Map() 是一种内置的 Python 函数,它<span style="color: black;">能够</span>将函数应用于<span style="color: black;">各样</span>数据结构中的元素,如列表或字典。<span style="color: black;">针对</span>这种运算<span style="color: black;">来讲</span>,这是一种非常干净<span style="color: black;">况且</span>可读的执行方式。</p>def square_it_func(a):
return a * a
x = map(square_it_func, )
print(x) # prints
def multiplier_func(a, b):
return a * b
x = map(multiplier_func, , )
print(x) # prints <span style="color: black;">瞧瞧</span>上面的示例!<span style="color: black;">咱们</span><span style="color: black;">能够</span>将函数应用于单个或多个列表。<span style="color: black;">实质</span>上,你<span style="color: black;">能够</span><span style="color: black;">运用</span>任何 Python 函数<span style="color: black;">做为</span> map 函数的输入,只要它与你正在操作的序列元素是兼容的。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Filter 函数</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filter 内置函数与 map 函数非常<span style="color: black;">类似</span>,它<span style="color: black;">亦</span>将函数应用于序列结构(列表、元组、字典)。二者的关键区别在于 filter() 将只返<span style="color: black;">回复</span>用函数返回 True 的元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">详情请看如下示例:</p># Our numbers
numbers =
# Function that filters out all numbers which are odd
def filter_odd_numbers(num):
if num % 2 == 0:
return True
else:
return False
filtered_numbers = filter(filter_odd_numbers, numbers)
print(filtered_numbers)
# filtered_numbers = <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>不仅<span style="color: black;">评定</span>了<span style="color: black;">每一个</span>列表元素的 True 或 False,filter() 函数还<span style="color: black;">保证</span>只返回匹配为 True 的元素。非常便于处理<span style="color: black;">检测</span>表达式和构建返回列表这两步。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Itertools 模块</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python 的 Itertools 模块是处理迭代器的工具集合。迭代器是一种<span style="color: black;">能够</span>在 for 循环语句(<span style="color: black;">包含</span>列表、元组和字典)中<span style="color: black;">运用</span>的数据类型。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span> Itertools 模块中的函数让你<span style="color: black;">能够</span>执行<span style="color: black;">非常多</span>迭代器操作,这些操作<span style="color: black;">一般</span><span style="color: black;">必须</span>多行函数和<span style="color: black;">繁杂</span>的列表理解。关于 Itertools 的神奇之处,请看以下示例:</p>from itertools import *
# Easy joining of two lists into a list of tuples
for i in izip(, ):
print i
# (a, 1)
# (b, 2)
# (c, 3)
# The count() function returns an interator that
# produces consecutive integers, forever. This
# one is great for adding indices next to your list
# elements for readability and convenience
for i in izip(count(1), ):
print i
# (1, Bob)
# (2, Emily)
# (3, Joe)
# The dropwhile() function returns an iterator that returns
# all the elements of the input which come after a certain
# condition becomes false for the first time.
def check_for_drop(x):
print Checking: , x
return (x > 5)
for i in dropwhile(should_drop, ):
print Result: , i
# Checking: 2
# Checking: 4
# Result: 6
# Result: 8
# Result: 10
# Result: 12
# The groupby() function is great for retrieving bunches
# of iterator elements which are the same or have similar
# properties
a = sorted()
for key, value in groupby(a):
print(key, value), end= )
# (1, )
# (2, )
# (3, )
# (4, )
# (5, )<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Generator 函数</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Generator 函数是一个类似迭代器的函数,即它<span style="color: black;">亦</span><span style="color: black;">能够</span>用在 for 循环语句中。这大大简化了你的代码,<span style="color: black;">况且</span>相比简单的 for 循环,它节省了<span style="color: black;">非常多</span>内存。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如</span>,<span style="color: black;">咱们</span>想把 1 到 1000 的所有数字相加,以下代码块的<span style="color: black;">第1</span>部分向你展示了<span style="color: black;">怎样</span><span style="color: black;">运用</span> for 循环来进行这一计算。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>列表很小,<span style="color: black;">例如</span> 1000 行,计算所需的内存还行。但<span style="color: black;">倘若</span>列表巨长,<span style="color: black;">例如</span>十亿浮点数,<span style="color: black;">这般</span>做就会<span style="color: black;">显现</span>问题了。<span style="color: black;">运用</span>这种 for 循环,内存中将<span style="color: black;">显现</span><span style="color: black;">海量</span>列表,但不是<span style="color: black;">每一个</span>人都<span style="color: black;">有没有</span>限的 RAM 来存储这么多东西的。Python 中的 range() 函数<span style="color: black;">亦</span>是这么干的,它在内存中构建列表。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">代码中第二部分展示了<span style="color: black;">运用</span> Python generator 函数对数字列表求和。generator 函数创建元素,并<span style="color: black;">仅在</span>必要时将其存储在内存中,即一次一个。这<span style="color: black;">寓意</span>着,<span style="color: black;">倘若</span>你要创建十亿浮点数,你只能一次一个地把它们存储在内存中!Python 2.x 中的 xrange() 函数<span style="color: black;">便是</span><span style="color: black;">运用</span> generator 来构建列表。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>例子说明:<span style="color: black;">倘若</span>你想为一个很大的范围生成列表,<span style="color: black;">那样</span>就<span style="color: black;">必须</span><span style="color: black;">运用</span> generator 函数。<span style="color: black;">倘若</span>你的内存有限,<span style="color: black;">例如</span><span style="color: black;">运用</span>移动设备或边缘计算,<span style="color: black;">运用</span>这一<span style="color: black;">办法</span>尤其重要。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">便是</span>说,<span style="color: black;">倘若</span>你想对列表进行多次迭代,并且它足够小,<span style="color: black;">能够</span>放进内存,那最好<span style="color: black;">运用</span> for 循环或 Python 2.x 中的 range 函数。<span style="color: black;">由于</span> generator 函数和 xrange 函数将会在你每次<span style="color: black;">拜访</span>它们时生成新的列表值,而 Python 2.x range 函数是静态的列表,<span style="color: black;">况且</span>整数<span style="color: black;">已然</span>置于内存中,以便快速<span style="color: black;">拜访</span>。</p># (1) Using a for loopv
numbers = list()
for i in range(1000):
numbers.append(i+1)
total = sum(numbers)
# (2) Using a generator
def generate_numbers(n):
num, numbers = 1, []
while num < n:
numbers.append(num)
num += 1
return numbers
total = sum(generate_numbers(1000))
# (3) range() vs xrange()
total = sum(range(1000 + 1))
total = sum(xrange(1000 + 1))
</div>
你的见解独到,让我受益匪浅,非常感谢。 这篇文章真的让我受益匪浅,外链发布感谢分享! 你的言辞如同繁星闪烁,点亮了我心中的夜空。
页:
[1]