Python 学习笔记
查看对象的反射属性
dir 列出所有属性、方法
getattr 获取字段
1 |
|
输入
- input()
lambda
- Python lambda表达式
- https://zhuanlan.zhihu.com/p/91137035
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19from functools import reduce
data = [(2, 2), (3, 4), (4, 1), (1, 3)]
print(data.sort(key=lambda x: x[0]))
print([x+1 for x in (1, 2, 3, 4)])
# x for x in data[] if condition=> []
print([x for x in (1, 2, 3, 4) if x % 2 == 0])
# data为可迭代对象
# map(lambda,data)=>map object
print(list(map(lambda x: x*x, (1, 2, 3, 4))))
# filter(lambda,data)=>filter object
print(list(filter(lambda x: x % 2 == 1, (1, 2, 3, 4))))
# reduce(lambda,data)
# 将data的前两个元素与给lambda使用,并将返回值和下一个元素继续联合使用
print(reduce(lambda x, y: x+y, (1, 2, 3, 4)))
# lambda argument_list:expersion
# https://zhuanlan.zhihu.com/p/91137035
# https://bbs.huaweicloud.com/blogs/363952
Time
1 |
|
多线程threading
Command执行
- Python - CMD命令实时输出
- https://mohen.blog.csdn.net/article/details/84560895
- https://python3-cookbook.readthedocs.io/zh_CN/latest/c13/p06_executing_external_command_and_get_its_output.html
- stdout
- 编码
- utf-8
- GB2312
1
2
3
4
5
6
7
8
9
10import subprocess
def sh(command):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
print(type(p))
lines = []
for line in iter(p.stdout.readline, b''):
line = line.strip().decode("utf-8")
print(">>>", line)
lines.append(line)
return lines
类型
- type(object)
- isinstance(object,type)
json
1 |
|
Glob
- /**配合recursive=True才能实现递归
- data = glob.glob(xxxpath**, recursive=True)
文件操作
- 创建多级目录:os.makedirs(“xx/xxx”)
- shutil.copyfile(‘xx.py’, ‘d:/xx.py’)
读写数据
- mode
- w+ 文本可读可写,覆盖
- wb 二进制可读可写,覆盖
- a 文本追加
- ab 二进制追加
- 中文写入
- encoding=”utf-8”
1
2
3
4with open("file.txt",mode="w",encoding="utf-8") as f:
f.write(text)
with open("file.txt",mode="r",encoding="utf-8") as f:
text = f.read()
- encoding=”utf-8”
三元表达式
a = x if x>y else y
Python格式化二进制、位运算
整型的四种表现形式:
2 进 制:以’0b’开头。例如:’0b11011’表示10进制的27
8 进 制:以’0o’开头。例如:’0o33’表示10进制的27
10进制:正常显示
16进制:以’0x’开头。例如:’0x1b’表示10进制的27
1 |
|
1 |
|
Python 学习笔记
https://automask.github.io/wild/2021/10/25/log/P_Python_Feature/