如果你可以以 Python 式的方式使用 Python,那么 Python 是一种优雅的语言。
但不管你有多资深,真正用 Python 写代码都需要一些时间。
本文将向你分享 11 个 Pythonic 技巧,让你的 Python 技能提升到一个新水平。
现在,我们就一起来看这些代码。
1、Python中的解构赋值
给变量赋值是编程的基本操作,Python 有一些语法糖来使过程更加优雅。
让我们看一个简单的例子:
a, *mid, b = [1, 2, 3, 4, 5, 6]
print(a, mid, b)
# 1 [2, 3, 4, 5] 6
如上所示,在一个星号的帮助下,mid 变量将中间的项目作为列表接收。
2、使用 Zip 函数聚合项目
Python 有一个惊人的内置函数,叫做 zip。顾名思义,zip 函数聚合来自不同迭代的项目,例如列表、元组或集合,并返回一个迭代器。
id = [1, 2, 3, 4]
leaders = ['Elon Mask', 'Tim Cook', 'Bill Gates', 'Yang Zhou']
sex = ['male', 'male', 'male', 'male']
record = zip(id, leaders, sex)
print(list(record))
# [(1, 'Elon Mask', 'male'), (2, 'Tim Cook', 'male'), (3, 'Bill Gates', 'male'), (4, 'Yang Zhou', 'male')]
3、在 Python 中正确使用 Lambda 函数
如果你喜欢单线,Lambda 函数是你的朋友,让我们在这里看一个经典的问题:
给你一个清单如下,你能把里面所有的奇数都打印出来吗?
很简单的,你只需要一行代码:
print(list(filter(lambda x: x % 2 == 1, numbers)))
# [1, 37, 43, 51, 83, 43]
4、使用下划线忽略变量
有时,一个变量是无用的,你不必费心给它起名字,你可以用下划线忽略它。
>>> L = [1,3,5,7]
>>> a, _, b, _ = L
>>> print(a, b)
1 5
5、利用列表理解的力量
同样,如果你是 Python 单行代码的粉丝,列表理解是你必须知道的,它具有将多个操作转换为一行代码的能力:
Genius = ["Jerry", "Jack", "tom", "yang"]
L1 = [name if name.startswith('y') else 'Not Genius' for name in Genius]
print(L1)
# ['Not Genius', 'Not Genius', 'Not Genius', 'yang']
6、放置一个占位符
有时,你需要定义一个函数但尚未弄清楚如何实现它,你可以在其中放置一个占位符:
def my_func():
pass
或者更可爱的方式:🙂
def my_func():
...
7、通过正则表达式处理文本
正则表达式 (regex) 是处理字符/字符串的强大工具,Python 内置的 re 模块提供了正则表达式的所有功能:
import re
txt = "Yang is so handsome!!!"
x = re.search("Elon", txt)
print(x)
#None
8、Python 中的 Map 和 Reduce
Python中有两个著名的高阶函数:map()和reduce()。
map()函数接收两个参数,一个是函数,另一个是Iterable,它依次将初始化函数应用于序列的每个元素,并将结果作为新的迭代器返回。
names = ['yAnG', 'MASk', 'thoMas', 'LISA']
names = map(str.capitalize, names)
print(list(names))
# ['Yang', 'Mask', 'Thomas', 'Lisa']
reduce()方法也有两个参数,一个是函数,另一个是Iterable,它将函数应用于序列,该函数必须接收两个参数,reduce 继续结果并与序列的下一个元素执行累积计算。最后,它返回累积计算的结果。
from functools import reduce
city = ['L', 'o', 'n', 'd', 'o', 'n', 2, 0, 2, 0]
city_to_str = reduce(lambda x, y: str(x) + str(y), city)
print(city_to_str)
# London2020
9、优雅地删除字符串中不必要的空格
这个字符串处理技巧结合了 splits() 和 join() 的强大功能:
quote = " Yang is a full stack hacker."
new_quote = ' '.join(quote.split())
print(new_quote)
# Yang is a full stack hacker.
10、浅拷贝列表的最简单方法
在 Python 中浅拷贝列表的最简单方法是利用列表切片的特性:
>>> a = [1, 2, 3, 4, 5, 6]
>>> b = a[:]
>>> b[0]=100
>>> b
[100, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3, 4, 5, 6]
11. 在解释器中检查最后一个表达式的值
这是 Python Interpreter 的一个有趣的小技巧,我们可以使用一个下划线来简单地获取最后一个表达式的值。
>>> 5+6
11
>>> _
11