python关键字yield

yield是python关键字
但是不是很会用
参考博客

需要明白
1:yield作用类似于return,但是不同于return
2:带yield的函数相当于一个生成器

def train():
    UCI_TRAIN_DATA=np.resize(np.arange(30),(10,3))

    def reader():
        for d in UCI_TRAIN_DATA:
            yield d[:-1], d[-1:]

    return reader

reader=train()
for i in reader():
    print(i)
输出:
(array([0, 1]), array([2]))
(array([3, 4]), array([5]))
(array([6, 7]), array([8]))
(array([ 9, 10]), array([11]))
(array([12, 13]), array([14]))
(array([15, 16]), array([17]))
(array([18, 19]), array([20]))
(array([21, 22]), array([23]))
(array([24, 25]), array([26]))
(array([27, 28]), array([29]))

你可能感兴趣的:(python,开发语言,人工智能)