Python
glob
import glob
#获取指定目录下的所有图片
print(glob.glob(r"data/test/*.png"), "\n")#加上r让字符串不转义
#获取上级目录的所有.py文件
print(glob.glob(r'../*.py')) #相对路径
reshape
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)
z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
模块存放位置
import sys, pprint
pprint.pprint(sys.path)
查看包内可以从外部调用的方法
import copy
[n for n in dir(copy) if not n.startswith('_')]
字典的创建
girls = ['alice', 'bernice', 'clarice']
letterGirls = {}
for girl in girls:
letterGirls.setdefault(girl[0], []).append(girl)
D = [1 / 10] * 10
print(D)
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
import torch
print("hello pytorch {}".format(torch.__version__))
print(torch.cuda.is_available())
hello pytorch 1.7.1+cpu
False # gpu不可用,当输出为True时可用