From a58118d3fbcfbeba3fb5aea53ede9db6c0822346 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Fri, 29 Mar 2024 12:31:49 +0800 Subject: [PATCH] update --- .../multiprocessing_example.py | 21 +++++----- .../numpy_example.py | 39 +++++++++++-------- .../pickle_example.py | 7 ++++ .../python_example.py | 9 +++++ .../re_example.py | 23 +++++++++++ .../threading_example.py | 13 +++++-- .../tqdm_example.py | 7 ++++ 7 files changed, 89 insertions(+), 30 deletions(-) create mode 100644 2022.03.16_frequently_used_python_package/pickle_example.py create mode 100644 2022.03.16_frequently_used_python_package/python_example.py create mode 100644 2022.03.16_frequently_used_python_package/re_example.py create mode 100644 2022.03.16_frequently_used_python_package/tqdm_example.py diff --git a/2022.03.16_frequently_used_python_package/multiprocessing_example.py b/2022.03.16_frequently_used_python_package/multiprocessing_example.py index f6759c5..c6d700b 100644 --- a/2022.03.16_frequently_used_python_package/multiprocessing_example.py +++ b/2022.03.16_frequently_used_python_package/multiprocessing_example.py @@ -2,22 +2,23 @@ from multiprocessing import Process import time def f(name): - time.sleep(5) - print('Hello', name) + for i in range(5): + time.sleep(1) + print('Hello', name, i) if __name__ == '__main__': start_time = time.time() - p1 = Process(target=f, args=('Bob',)) - p2 = Process(target=f, args=('Alice',)) + f('A') + f('B') + end_time = time.time() + print(end_time - start_time, '\n') + + start_time = time.time() + p1 = Process(target=f, args=('A',)) + p2 = Process(target=f, args=('B',)) p1.start() p2.start() p1.join() p2.join() end_time = time.time() - print(end_time - start_time, '\n') - - start_time = time.time() - f('Bob') - f('Alice') - end_time = time.time() print(end_time - start_time) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/numpy_example.py b/2022.03.16_frequently_used_python_package/numpy_example.py index 64e20ae..77e3f0c 100644 --- a/2022.03.16_frequently_used_python_package/numpy_example.py +++ b/2022.03.16_frequently_used_python_package/numpy_example.py @@ -1,21 +1,26 @@ import numpy as np -np.zeros((2, 3)) # 零矩阵 -np.identity(3) # 单位矩阵 -np.diag([1, 3, 5]) # 对角矩阵 +print(np.zeros((2, 3)), '\n') # 零矩阵 +print(np.identity(3), '\n') # 单位矩阵 +print(np.diag([1, 3, 5]), '\n') # 对角矩阵 matrix1 = np.array([[3, 5+1j], [2, 7]]) # numpy矩阵 -matrix1.shape # 矩阵的维度 -matrix1.transpose() # 矩阵转置 -matrix1.conj() # 矩阵所有元素共轭 -np.conj(matrix1) # 矩阵所有元素共轭(同上) -np.arange(1, 5, 1) # 数列(左闭右开) -np.linspace(-2, 2, 5) # 数列(左闭右闭) -np.random.uniform(-2, 2) # 随机数 -np.random.randint(0, 2) # 随机整数(左闭右开) -np.sort([1, 7, 0, 3]) # 排列 -np.argsort([1, 7, 0, 3]) # 排列索引 -np.linalg.det(matrix1) # 行列式 +print(matrix1, '\n') +print(matrix1.shape, '\n') # 矩阵的维度 +print(matrix1.transpose(), '\n') # 矩阵转置 +print(matrix1.conj(), '\n') # 矩阵所有元素共轭 +print(np.conj(matrix1), '\n') # 矩阵所有元素共轭(同上) +print(np.arange(1, 5, 1), '\n') # 数列(左闭右开) +print(np.linspace(-2, 2, 5), '\n') # 数列(左闭右闭) +print(np.random.uniform(-2, 2), '\n') # 随机数 +print(np.random.randint(0, 2), '\n') # 随机整数(左闭右开) +print(np.sort([1, 7, 0, 3]), '\n') # 排列 +print(np.argsort([1, 7, 0, 3]), '\n') # 排列索引 +print(np.linalg.det(matrix1), '\n') # 行列式 matrix2 = np.linalg.inv(matrix1) # 求逆 -np.matmul(matrix1, matrix2) # 矩阵乘积 -np.dot(matrix1, matrix2) # 矩阵乘积(同上) +print(matrix2, '\n') +print(np.matmul(matrix1, matrix2), '\n') # 矩阵乘积 +print(np.dot(matrix1, matrix2), '\n') # 矩阵乘积(同上) eigenvalue, eigenvector = np.linalg.eig(matrix1) # 求本征值,本征向量 -matrix3 = np.append(matrix1, matrix2, axis=0) # 增加数组元素或者矩阵的行 \ No newline at end of file +print(eigenvalue, '\n') +print(eigenvector, '\n') +matrix3 = np.append(matrix1, matrix2, axis=0) # 增加数组元素或者矩阵的行 +print(matrix3) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/pickle_example.py b/2022.03.16_frequently_used_python_package/pickle_example.py new file mode 100644 index 0000000..b5d9487 --- /dev/null +++ b/2022.03.16_frequently_used_python_package/pickle_example.py @@ -0,0 +1,7 @@ +import pickle +data = [1, 2, 3] +with open('a.txt', 'wb') as f: + pickle.dump(data, f) +with open('a.txt', 'rb') as f: + data_load = pickle.load(f) +print(data_load) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/python_example.py b/2022.03.16_frequently_used_python_package/python_example.py new file mode 100644 index 0000000..06df20c --- /dev/null +++ b/2022.03.16_frequently_used_python_package/python_example.py @@ -0,0 +1,9 @@ +a = [1, 2] # 数组 +print(a) +print(len(a)) # 数组长度 +a.append(3) # 增加元素 +print(a) +b = range(5) # 数列(从0开始) +print(b) +for i0 in b: + print(i0) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/re_example.py b/2022.03.16_frequently_used_python_package/re_example.py new file mode 100644 index 0000000..906bb12 --- /dev/null +++ b/2022.03.16_frequently_used_python_package/re_example.py @@ -0,0 +1,23 @@ +import re + +# 正则表达式:匹配任意字符(.);零次或多次(*);尽可能少地匹配(?) + +# re.match 指匹配起始位置的模式,如果起始位置不匹配的话,返回 None +string = 'this_is_a_test_abc_123' +result = re.match(pattern='ab(.*?)23', string=string) +print(result) +print() +result = re.match(pattern='this(.*?)abc', string=string) +print(result.group(0)) +print(result.group(1)) +print() + +# re.search 扫描整个字符串并返回第一个成功的匹配。 +string = 'this_is_a_test_abc_123' +result = re.search(pattern='ab(.*?)23', string=string) +print(result.group(0)) +print(result.group(1)) +print() +result = re.search(pattern='this(.*?)abc', string=string) +print(result.group(0)) +print(result.group(1)) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/threading_example.py b/2022.03.16_frequently_used_python_package/threading_example.py index c465f55..d9a9f6d 100644 --- a/2022.03.16_frequently_used_python_package/threading_example.py +++ b/2022.03.16_frequently_used_python_package/threading_example.py @@ -6,16 +6,23 @@ def print_numbers(x): time.sleep(1) print(f"Thread {x}: {i}") +# 串行测试 +start_time = time.time() +print_numbers('A') +print_numbers('B') +end_time = time.time() +print(end_time - start_time, '\n') + # 创建两个线程 thread1 = threading.Thread(target=print_numbers, args=('A')) thread2 = threading.Thread(target=print_numbers, args=('B')) +start_time = time.time() # 启动线程 thread1.start() thread2.start() - # 等待两个线程完成 thread1.join() thread2.join() - -print("Both threads have finished.") \ No newline at end of file +end_time = time.time() +print(end_time - start_time) \ No newline at end of file diff --git a/2022.03.16_frequently_used_python_package/tqdm_example.py b/2022.03.16_frequently_used_python_package/tqdm_example.py new file mode 100644 index 0000000..b38af54 --- /dev/null +++ b/2022.03.16_frequently_used_python_package/tqdm_example.py @@ -0,0 +1,7 @@ +from tqdm import tqdm +import time +import numpy as np + +for i in tqdm(np.arange(1, 10)): + time.sleep(0.1) + print('', i) \ No newline at end of file