使用产量打印输出 [英] using yield print output

查看:153
本文介绍了使用产量打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此处的延续.

我正在使用yield语句而不是return.

I am using yield statement instead of return.

这是代码:

class Measurements():
    def __init__(self, value, other):
        self.value = value
        self.other = other


class Criteria():
    def __init__(self, new_value, measurements):
        self.new_value = new_value
        self.measurements = measurements

    def method(self):
        for measurement in self.measurements:
            if 20 < measurement.value < 110:
                measurement.value = self.new_value
        return self.measurements

class Evaluate():
    def __init__(self, criteria):
        self.criteria = criteria

    def execute(self):
        for c in self.criteria:
            c.method()
            yield c.measurements


def main():
    criteria = [
        Criteria(999, [Measurements(100, 0.3), Measurements(33, 0.5)]),
        Criteria(999, [Measurements(150, 0.3), Measurements(35, 0.5)]),
    ]

    compare =  [
        Measurements(999, 0.3), Measurements(999, 0.5),
        Measurements(100, 0.3), Measurements(999, 0.5)
    ]

    obs = Evaluate(criteria).execute()

    # here compare

if __name__ == "__main__":
    main()

我想将obs中的输出值与compare中的值进行比较.我指的是Measurements部分.

I want to compare my output values from obs with the values in the compare. I am refering to the Measurements part.

因此,从obs开始,我们有(对于运行代码后的变量值):999,999,150,999(因为如果20

,在compare中,我们有:999,999,100,999

So, from obs, we have (for the variable value after running the code) :999,999,150,999 ( because if 20

and from compare we have: 999,999,100,999

推荐答案

对于要执行哪些检查仍然不确定,但这是一个使您入门的示例.进行了几处更改

Still a bit unsure on what checks you wanted to perform, but here is an example that should get you started. Couple of changes were made

# Made compare a list contain lists of Measurements to match criteria
compare =  [
    [Measurements(999, 0.3), Measurements(999, 0.5)],
    [Measurements(100, 0.3), Measurements(999, 0.5)]
]


# Added __repr__ method to Measurement class
def __repr__(self):
    return '{0} {1}'.format(self.value, self.other)

我建议您在有类实例列表的情况下执行此操作,这会使调试变得更加容易,而不是得到更有意义的东西.

I suggest doing this whenever you have a list of class instances, it makes debugging much easier as instead of getting this, you get something more meaningful.

<__main__.Measurements object at 0x0000000003E2C438>


现在用于比较值了,我使用zip将两个列表组合在一起,使比较值更加容易.然后对于内部for循环,我们再次将来自每个组的嵌套列表压缩在一起.从这里开始,每个项目都是一个度量,我们可以检查其值.


Now for comparing the values I used zip to group the two lists together making it easier to compare the values. Then for the inner for loop we again zip the nested lists from each group together. From here each item is a Measurement that we can check their values of.

for crit_lst, comp_lst in zip(obs, compare):
    for crit_meas, comp_meas in zip(crit_lst, comp_lst):
        print(crit_meas, comp_meas)
        if crit_meas.value != comp_meas.value: # example of comparing their values
            print('Mis-Match', crit_meas.value, comp_meas.value)

这篇关于使用产量打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆