三元运算子
>>> a,b=2,3
>>> min=a if a<b else b
>>> min
2
随机数:
import numpy as np
img_noise = np.random.randint(1, 9, size=(2, 5))
print(img_noise)
output:
[[7 5 7 6 3]
[3 4 2 4 2]]
求列表平均数:
import numpy as np
c = np.array([[4.,5], [5.,6], [6.,7]])
print(np.mean(c, 0))
c = np.array([[4.,5], [5.,6], [6.,7]])
print(np.mean(c, 1))
output:
[5. 6.]
[4.5 5.5 6.5]
打印列表[1,2,3,4,5,6]中能被3整除的数:
l = [x for x in [1,2,3,4,5,6] if x%3==0]
print(l)
打印100以内能被3整除的数(使用filter):
c = list(filter(lambda x:True if x % 3 == 0 else False, range(100)))
print(c)
打印均值为1, 标准差为0.1的10个数:
import numpy as np
vals = np.random.normal(1, 0.1, 10)
print(vals)