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]))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值