Home > Backend Development > Python Tutorial > 20 Python usage tips, recommended to collect!

20 Python usage tips, recommended to collect!

Release: 2023-08-09 17:42:31
forward
777 people have browsed it


1. Easy to confuse operations

This section compares some confusing operations in Python.

1.1 Random sampling with replacement and random sampling without replacement

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

1.2 Lambda function Parameters

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

1.3 copy and deepcopy

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

When combined with variable aliases, it is easy to Confusion:

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]
Copy after login

Modification of the alias will affect the original variable. The elements in the (shallow) copy are aliases of the elements in the original list, while the deep copy copies recursively. Copied modifications do not affect the original variables.

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})
Copy after login

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

import sys
csv.field_size_limit(sys.maxsize)
Copy after login

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

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

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
Copy after login

序列排序:

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
Copy after login

多个序列合并:

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)  # 当最长的序列耗尽时停止,结果只能被消耗一次
Copy after login

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)
Copy after login

2.4 带默认值的 Dict

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

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

2.5 有序 Dict

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

3、高性能编程和调试

3.1 输出错误和警告信息

向标准错误输出信息

import sys
sys.stderr.write(&#39;&#39;)
Copy after login

输出警告信息

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

控制警告消息的输出

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

3.2 代码中测试

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

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

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

$ python -0 main.py
Copy after login

3.3 代码风格检查

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

pylint main.py
Copy after login

3.4 代码耗时

耗时测试

$ python -m cProfile main.py
Copy after login

测试某代码块耗时

# 代码块耗时定义
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
Copy after login

代码耗时优化的一些原则

  • Focus on optimizing where performance bottlenecks occur, not the entire code.
  • Avoid using global variables. Local variables are looked up faster than global variables, and running code defining global variables within a function is typically 15%-30% faster.
  • #Avoid using . to access properties. It will be faster to use from module import name to put the frequently accessed class member variable self.member into a local variable.
  • # Try to use built-in data structures. str, list, set, dict, etc. are implemented in C and run very fast.
  • # Avoid creating unnecessary intermediate variables, and copy.deepcopy().
  • String concatenation, such as a ':' b ':' c will create a large number of useless intermediate variables, ':',join([a, b , c]) The efficiency will be much higher. In addition, you need to consider whether string concatenation is necessary. For example, print(':'.join([a, b, c])) is less efficient than print(a, b, c, sep=':').

The above is the detailed content of 20 Python usage tips, recommended to collect!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Python当打之年
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template