Python学习记录

一、常用的切片操作

  • 下采样,即间隔取值
    ref_img = item[0][0][:, ::4, ::4]
    src_img = item[0][1][:, ::4, ::4]
  • 反转数组
src_img_tmp = src_img.permute(1, 2, 0).numpy()[:, :, ::-1]

二、range函数
range(start, stop[, step]) [start, stop) 区间左闭右开,同切片操作

三、Python写文件操作

Ref. How to write content in a file using Python

四、Python字符串包含

五、assert 断言
表达式为False时触发异常AssertionError。不满足条件时直接返回错误,而不是等程序奔溃后退出。

六、路径不存在则创建
python mkdir if not exist

import os
if not os.path.exists(xx_path):
    os.mkdir()
    os.makedirs()

七、获取路径下的所有文件
os.listdir(your_path)

八、Python写csv文件
Ref. https://www.geeksforgeeks.org/writing-csv-files-in-python/
csvwriter只有writerow方法,没有writecol。原因应该是逗号分隔是行内分隔。

九、字符串大小写
转为小写lower(), 转为大写upper()

Ref. https://www.cnblogs.com/cmnz/p/6956984.html

十、Matplotlib
Ref. https://chartio.com/resources/tutorials/how-to-save-a-plot-to-a-file-using-matplotlib/

十一、Python之filter函数

说明:7个一组(06)、(713), 取出每一组中间的那个值,即3, 10, 17等等

原文地址:https://www.cnblogs.com/Todd-Qi/p/10685376.html