python 常用的几个代码段

1 读txt 文件

with open("test.txt",'r')as f:
    for line in f:
        print(line.rstrip('\n'))

2 PIL 图像读取并用plt显示

from  PIL import Image
import matplotlib.pyplot as plt
import numpy as np

image = Image.open("1.jpg")

image = np.array(image)
print(type(image))
print(image.shape)
image = image[:,:,2]


plt.imshow(image)
plt.show()

3 plt 在同一张图像上处理多张图像

import matplotlib.pyplot as plt
plt.figure("image")
plt.subplot(1,2,1)
plt.imshow(image)
plt.subplot(1,2,2)
plt.imshow(lable)
plt.show()

4  连接

import torch

a =[[1,2,3],[4,5,6]]
b =[[1,2,3],[4,5,6]]


a1 = torch.tensor(a)
b1 = torch.tensor(b)

c1 = a1 + b1
c2 = torch.cat([a1,b1],0)
c3 = a + b

print(c1)
print(c2)
print(c3)

5 view max

x = x.view(x.size(0),-1)
_,pre = outputs.max(1)

6  sort 和 lambda表达式

lambda 相当于函数

self.imgs = sorted(imgs, key=lambda x: int(x.split('.')[-2]))

 

你可能感兴趣的:(python)