首頁  >  文章  >  後端開發  >  20個Python使用小技巧,建議收藏!

20個Python使用小技巧,建議收藏!

Python当打之年
Python当打之年轉載
2023-08-09 17:42:31698瀏覽


1、易混淆運算

本節將一些Python 易混淆的操作進行比較。

1.1 有放回隨機取樣和無放回隨機取樣

import random
random.choices(seq, k=1)  # 长度为k的list,有放回采样
random.sample(seq, k)     # 长度为k的list,无放回采样

1.2 lambda 函數的參數

func = lambda y: x + y          # x的值在函数运行时被绑定
func = lambda y, x=x: x + y     # x的值在函数定义时被绑定

1.3 copy 和deepcopy

import copy
y = copy.copy(x)      # 只复制最顶层
y = copy.deepcopy(x)  # 复制所有嵌套部分

複製和變數別名結合在一起時,容易混淆:

a = [1, 2, [3, 4]]

# Alias.
b_alias = a  
assert b_alias == a and b_alias is a

# Shallow copy.
b_shallow_copy = a[:]  
assert b_shallow_copy == a and b_shallow_copy is not a and b_shallow_copy[2] is a[2]

# Deep copy.
import copy
b_deep_copy = copy.deepcopy(a)  
assert b_deep_copy == a and b_deep_copy is not a and b_deep_copy[2] is not a[2]

對別名的修改會影響原變量,(淺)複製中的元素是原始列表中元素的別名,而深層複製是遞歸的進行複製,對深層複製的修改不影響原變數。

2、常用工具

2.1 读写 CSV 文件

import csv
# 无header的读写
with open(name, 'rt', encoding='utf-8', newline='') as f:  # newline=''让Python不将换行统一处理
    for row in csv.reader(f):
        print(row[0], row[1])  # CSV读到的数据都是str类型
with open(name, mode='wt') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(['symbol', 'change'])

# 有header的读写
with open(name, mode='rt', newline='') as f:
    for row in csv.DictReader(f):
        print(row['symbol'], row['change'])
with open(name, mode='wt') as f:
    header = ['symbol', 'change']
    f_csv = csv.DictWriter(f, header)
    f_csv.writeheader()
    f_csv.writerow({'symbol': xx, 'change': xx})

注意,当 CSV 文件过大时会报错:_csv.Error: field larger than field limit (131072),通过修改上限解决

import sys
csv.field_size_limit(sys.maxsize)

csv 还可以读以 \t 分割的数据

f = csv.reader(f, delimiter='\t')

2.2 迭代器工具

itertools 中定义了很多迭代器工具,例如子序列工具:

import itertools
itertools.islice(iterable, start=None, stop, step=None)
# islice('ABCDEF', 2, None) -> C, D, E, F

itertools.filterfalse(predicate, iterable)         # 过滤掉predicate为False的元素
# filterfalse(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 6

itertools.takewhile(predicate, iterable)           # 当predicate为False时停止迭代
# takewhile(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 1, 4

itertools.dropwhile(predicate, iterable)           # 当predicate为False时开始迭代
# dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 6, 4, 1

itertools.compress(iterable, selectors)            # 根据selectors每个元素是True或False进行选择
# compress(&#39;ABCDEF&#39;, [1, 0, 1, 0, 1, 1]) -> A, C, E, F

序列排序:

sorted(iterable, key=None, reverse=False)

itertools.groupby(iterable, key=None)              # 按值分组,iterable需要先被排序
# groupby(sorted([1, 4, 6, 4, 1])) -> (1, iter1), (4, iter4), (6, iter6)

itertools.permutations(iterable, r=None)           # 排列,返回值是Tuple
# permutations(&#39;ABCD&#39;, 2) -> AB, AC, AD, BA, BC, BD, CA, CB, CD, DA, DB, DC

itertools.combinations(iterable, r=None)           # 组合,返回值是Tuple
itertools.combinations_with_replacement(...)
# combinations(&#39;ABCD&#39;, 2) -> AB, AC, AD, BC, BD, CD

多个序列合并:

itertools.chain(*iterables)                        # 多个序列直接拼接
# chain(&#39;ABC&#39;, &#39;DEF&#39;) -> A, B, C, D, E, F

import heapq
heapq.merge(*iterables, key=None, reverse=False)   # 多个序列按顺序拼接
# merge(&#39;ABF&#39;, &#39;CDE&#39;) -> A, B, C, D, E, F

zip(*iterables)                                    # 当最短的序列耗尽时停止,结果只能被消耗一次
itertools.zip_longest(*iterables, fillvalue=None)  # 当最长的序列耗尽时停止,结果只能被消耗一次

2.3 计数器

计数器可以统计一个可迭代对象中每个元素出现的次数。

import collections
# 创建
collections.Counter(iterable)

# 频次
collections.Counter[key]                 # key出现频次
# 返回n个出现频次最高的元素和其对应出现频次,如果n为None,返回所有元素
collections.Counter.most_common(n=None)

# 插入/更新
collections.Counter.update(iterable)
counter1 + counter2; counter1 - counter2  # counter加减

# 检查两个字符串的组成元素是否相同
collections.Counter(list1) == collections.Counter(list2)

2.4 带默认值的 Dict

当访问不存在的 Key 时,defaultdict 会将其设置为某个默认值。

import collections
collections.defaultdict(type)  # 当第一次访问dict[key]时,会无参数调用type,给dict[key]提供一个初始值

2.5 有序 Dict

import collections
collections.OrderedDict(items=None)  # 迭代时保留原始插入顺序

3、高性能编程和调试

3.1 输出错误和警告信息

向标准错误输出信息

import sys
sys.stderr.write(&#39;&#39;)

输出警告信息

import warnings
warnings.warn(message, category=UserWarning)  
# category的取值有DeprecationWarning, SyntaxWarning, RuntimeWarning, ResourceWarning, FutureWarning

控制警告消息的输出

$ python -W all     # 输出所有警告,等同于设置warnings.simplefilter(&#39;always&#39;)
$ python -W ignore  # 忽略所有警告,等同于设置warnings.simplefilter(&#39;ignore&#39;)
$ python -W error   # 将所有警告转换为异常,等同于设置warnings.simplefilter(&#39;error&#39;)

3.2 代码中测试

有时为了调试,我们想在代码中加一些代码,通常是一些 print 语句,可以写为:

# 在代码中的debug部分
if __debug__:
    pass

一旦调试结束,通过在命令行执行 -O 选项,会忽略这部分代码:

$ python -0 main.py

3.3 代码风格检查

使用 pylint 可以进行不少的代码风格和语法检查,能在运行之前发现一些错误

pylint main.py

3.4 代码耗时

耗时测试

$ python -m cProfile main.py

测试某代码块耗时

# 代码块耗时定义
from contextlib import contextmanager
from time import perf_counter

@contextmanager
def timeblock(label):
    tic = perf_counter()
    try:
        yield
    finally:
        toc = perf_counter()
        print(&#39;%s : %s&#39; % (label, toc - tic))

# 代码块耗时测试
with timeblock(&#39;counting&#39;):
    pass

代码耗时优化的一些原则

  • 專注於最佳化產生效能瓶頸的地方,而不是全部程式碼。
  • 避免使用全域變數。局部變數的查找比全域變數更快,將全域變數的程式碼定義在函數中運行通常會快 15%-30%。
  • 避免使用。存取屬性。使用 from module import name 會更快,將頻繁存取的類別的成員變數 self.member 放入到一個局部變數中。
  • 盡量使用內建資料結構。 str, list, set, dict 等使用 C 實現,運行起來很快。
  • 避免創建沒有必要的中間變量,和 copy.deepcopy()。
  • 字串拼接,例如a ':' b ':' c 會創造大量無用的中間變量,':',join([a, b , c]) 效率會高不少。另外需要考慮字串拼接是否必要,例如 print(':'.join([a, b, c])) 效率比 print(a, b, c, sep=':') 低。
#

以上是20個Python使用小技巧,建議收藏!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:Python当打之年。如有侵權,請聯絡admin@php.cn刪除