¶ 语法
缩进应该使用4个空格而不是tab:https://peps.python.org/pep-0008/#tabs-or-spaces
¶ for循环
0到9循环:
for i in range(10) :
Statements
在交互式环境中(如命令行形式)后面要多打一个回车才开始运行
¶ 类
from dataclasses import dataclass
@dataclass
class A:
int
a: str
b: float
c: list[A] = []
li: 1, "2", 3.3))
li.append(A(2, "3", 4.4))
li.append(A(print(li)
# [<class '__main__.A'>, A(a=1, b='2', c=3.3), A(a=2, b='3', c=4.4)]
= li[1]
item print(item.c)
# 3.3
sorted(li, key = lambda item: item.c, reverse=True)
参考:https://stackoverflow.com/questions/35988/c-like-structures-in-python
完整教程:Python 面向对象
¶ 内置数据结构
¶ 输入
¶ 输出
¶ 输出为科学计数法
参考:https://blog.csdn.net/qq_45434742/article/details/102094577?fps=1&locationNum=2
print("%e" %111)
= 111
x print("%e" %x)
print("%e" %(111 + 111))
print("%e %e" %(x, 111 + 111))
¶ 不打印换行符
print('hello', end='')
¶ 打印到stderr
print('xxx', file=sys.stderr)
来源:https://stackoverflow.com/questions/5574702/how-do-i-print-to-stderr-in-python
¶ 指定分隔符打印数组
>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5
来源:https://stackoverflow.com/questions/22556449/print-a-list-of-space-separated-elements
¶ 文件
python文件管理¶ 切换到脚本所在目录
import sys
import os
= os.path.abspath(sys.argv[0])
abspath = os.path.dirname(abspath)
dname os.chdir(dname)
¶ 标准库
¶ 时间
https://docs.python.org/3/library/datetime.html
1516332287) datetime.datetime.fromtimestamp(
也可以用time
包:Python中时间与时间戳之间的转换
¶ math
¶ 阶乘
10!
from math import *
10) factorial(
¶ 正则
https://docs.python.org/3/library/re.html
¶ 执行命令
¶ 简单执行命令
import subprocess
'./executable', arg1, arg2]) subprocess.call([
来源:https://stackoverflow.com/questions/5788891/execute-a-file-with-arguments-in-python-shell
¶ 获取命令输出
¶ 排序
用sorted
。
¶ 对字典的key排序
= {1: 2, 4: 3, 2: 1}
x sorted(x)
[1, 2, 4]
¶ 对字典的值排序
= {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
x sorted(x.items(), key=lambda item: item[1])
[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
来源:https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
¶ shlex
可以用解析命令行参数的方式解析字符串。文档:https://docs.python.org/3/library/shlex.html
例子:
# https://stackoverflow.com/a/899314
import shlex
'-o 1 --long "Some long string"') shlex.split(
['-o', '1', '--long', 'Some long string']
¶ 其他
Waiting for I/O completion: https://docs.python.org/3/library/select.html
字符串trim: https://www.freecodecamp.org/news/python-strip-how-to-trim-a-string-or-line/
¶ 三方库
¶ dateutil
https://dateutil.readthedocs.io/en/stable/
pip3 install python-dateutil
可以实现时间减去月数等功能。
来源:https://thispointer.com/subtract-months-from-a-date-in-python/
¶ pandas
官方文档:https://pandas.pydata.org/docs/
pandas数据框常用操作¶ numpy
官方文档:https://numpy.org/doc/stable/
¶ 矩阵操作
¶ 等差数列
文档:https://numpy.org/doc/stable/reference/generated/numpy.linspace.html
=50, endpoint=True, retstep=False, dtype=None, axis=0)[source] numpy.linspace(start, stop, num
¶ 其他
json.dumps(obj, indent='\t')
可以使用tab进行缩进。
¶ pip
安装指定版本:https://www.marsja.se/pip-install-specific-version-of-python-package/
requirements.txt: https://note.nkmk.me/en/python-pip-install-requirements/
¶ 有关
¶ 已知的问题
调用另一个文件里的函数不太方便: https://www.geeksforgeeks.org/python-import-module-from-different-directory/