16 4 2 2 1 1 python articles

Python常用代码

Dict按value排序 1 sorted_obj = {k: v for k, v in sorted(obj.items(), key=lambda item: item[1], reverse=True)} 多线程执行,注意结果的获取方式 1 2 3 4 5 6 7 8 9 10 11 12 from multiprocessing import Pool import os def f(x): print('Child process id:', os.getpid()) return x*2 if __name__ == '__main__': with Pool(5) as p: results = p.map(f, [1, 2, 3]) print(results)……

Continue reading

Python操作Excel常用代码

读Excel,方法一: 1 2 3 4 5 6 7 8 9 10 import xlrd from tqdm import tqdm workbook = xlrd.open_workbook('some.xlsx') sheet = workbook.sheet_by_name('sheet1') #sheet = workbook.sheet_by_index(0) nrows = sheet.nrows for idx in tqdm(range(1, nrows)): data = sheet.cell_value(idx, 0) 写Excel,方法一(支持xls,小于6w行数): 1 2 3 4 5 6 7 8 9 10 11 12 13 import xlwt workbook = xlwt.Workbook() sheet = workbook.add_sheet('sheet1') sheet.write(0, 0, 'Head_1') idx = 1 for row in tqdm(datas): sheet.write(idx, 0, row['data_1']) idx += 1 workbook.save('some.xls') 写Excel,方法二(支持xlsx,大于6w行数): 1 2 3……

Continue reading

正则表达式常用代码

全句匹配: 1 2 3 import re is_match = re.match(r"[regex_str]*", "match_str") 部分匹配: 1 2 3 4 5 6 7 8 9 10 import re matchObj = re.search(r"[regex_str]*", "match_str") if matchObj: print("matchObj.group() : {}".format(matchObj.group())) print("matchObj.group(1) : {}".format(matchObj.group(1))) print("matchObj.group(2) : {}".format(matchObj.group(2))) else: print("No match!!") 正则替换: 1 2 3 import re replace_result = re.sub(r"[regex_str]*", "replace_to", "match_str")……

Continue reading

Jupyter添加kernel

1、切换到对应的虚拟环境,安装ipykernel 1 pip install ipykernel 2、注册kernel 1 python -m ipykernel install --user --name <name> --display-name "Python (name)" 3、删除kernel 1 2 jupyter kernelspec list jupyter kernelspec remove <name>……

Continue reading

conda配置清华源

添加清华源: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cat > ~/.condarc << EOF channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud EOF 创建虚拟环境 1 2 conda create -n ENV-NAME python=3.8 -y conda activate ENV-NAME 删除虚拟环境 1 2 3 conda deactivate conda env remove -n ENV-NAME conda env list 初始化base环境 1 2 conda list --revisions conda install --revisions REV_NUM 清理缓存 1 conda clean -a -y……

Continue reading

pip配置清华源

方法一: 1 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 方法二: 1 2 3 4 5 6 7 8 9 mkdir ~/.pip cat > ~/.pip/pip.conf << EOF [global] trusted-host=pypi.tuna.tsinghua.edu.cn index-url=https://pypi.tuna.tsinghua.edu.cn/simple EOF 安装pip时加速: 1 python3 -i https://pypi.tuna.tsinghua.edu.cn/simple get-pip.py……

Continue reading