Python循环一行写法

一个简单的例子

n = 5
a = np.array([i for i in range(n)])
print(a)
>>> output: [0 1 2 3 4]

循环中加入if

X = np.array([1, 2, 3, 4])
Y = [X[i] for i in range(len(X)) if X[i] % 2 == 0]
print(Y)
>>> output: [2, 4]
发布了19 篇原创文章 · 获赞 3 · 访问量 737

猜你喜欢

转载自blog.csdn.net/weixin_43486780/article/details/104301874