pytorch或python基础积累

1. python中有一个zfill方法用来给字符串前面补0,非常有用

= "123"
= n.zfill(5)
assert == "00123"
zfill()也可以给负数补0
= "-123"
= n.zfill(5)
assert == "-0123"
对于纯数字,我们也可以通过格式化的方式来补0
= 123
= "%05d" % n
assert == "00123"
2. int, string 之间的转换

astr = str(a) # a is a int
i = int(astr)
3. Tensor, numpy的array之间的转换

b = a.numpy() #a为tensor
b = torch.from_numpy(a)  #a为numpy的array
参考:https://www.jianshu.com/p/5ae644748f21







猜你喜欢

转载自blog.csdn.net/gaoprincess/article/details/79773175
今日推荐