使用return而不是yield

0 python csv class

回报是否好于收益?从我读过它可以.在这种情况下,我无法从if语句获取迭代.基本上该程序所做的是取两点,即开始和结束.如果这两个点相距至少十英里,则需要随机抽样.显示的最终if语句适用于从开始点开始的前20英里,begMi.nCounter.length = 10并且是一个类成员.所以问题是,如何将代码调整到return语句的工作位置而不是yield?或者在这种情况下收益率是否合理?

def yielderOut(self):
    import math
    import random as r
    for col in self.fileData:
        corridor = str(col['CORRIDOR_CODE'])
        begMi = float(col['BEGIN_MI'])
        endMi = float(col['END_MI'])
        roughDiff = abs(begMi - endMi)

        # if the plain distance between two points is greater than length = 10
        if roughDiff > nCounter.length:
            diff = ((int(math.ceil(roughDiff/10.0))*10)-10)
            if diff > 0 and (diff % 2 == 0 or diff % 3 == 0 or diff % 5 == 0)\
               and ((diff % roughDiff) >= diff):

                if (nCounter.length+begMi) < endMi:
                    vars1 = round(r.uniform(begMi,\
                    (begMi+nCounter.length)),nCounter.rounder)
                    yield corridor,begMi,endMi,'Output 1',vars1

                if ((2*nCounter.length)+begMi) < endMi:
                    vars2 = round(r.uniform((begMi+nCounter.length),\
                    (begMi+ (nCounter.length*2))),nCounter.rounder)
                    yield corridor,begMi,endMi,'Output 2',vars1,vars2

因此,roughdiff等于两点之间的差异,并向下舍入到最接近的十.然后减去10,所以样本取自整整十英里的部分; 这变得不同.因此,假设将24的roughDiff四舍五入为20,20-10,diff +开始点=样本取自mi 60和70之间而不是70到80之间.

该程序有效,但我认为如果我使用return而不是yield会更好.不是程序员.

Bryan Oakley.. 6

return不是更好,它是不同的.return说"我完成了.这是结果".yield说"这是一系列价值观中的下一个价值"

使用最能表达您意图的那个.