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

20 Python usage tips, recommended to collect!

Aug 09, 2023 pm 05:42 PM
python


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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

How to view the original functions encapsulated internally by Python functools.partial object? How to view the original functions encapsulated internally by Python functools.partial object? Apr 01, 2025 pm 04:15 PM

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Do Google and AWS provide public PyPI image sources? Do Google and AWS provide public PyPI image sources? Apr 01, 2025 pm 05:15 PM

Many developers rely on PyPI (PythonPackageIndex)...

How to efficiently count and sort large product data sets in Python? How to efficiently count and sort large product data sets in Python? Apr 01, 2025 pm 08:03 PM

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

How to optimize processing of high-resolution images in Python to find precise white circular areas? How to optimize processing of high-resolution images in Python to find precise white circular areas? Apr 01, 2025 pm 06:12 PM

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...

See all articles