Skip to content Skip to sidebar Skip to footer

Using Yield Print Output

This is a continuation from here. I am using yield statement instead of return. This is the code: class Measurements(): def __init__(self, value, other): self.value = v

Solution 1:

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 criteriacompare =  [
    [Measurements(999, 0.3), Measurements(999, 0.5)],
    [Measurements(100, 0.3), Measurements(999, 0.5)]
]

# Added __repr__ method to Measurement classdef__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>

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 inzip(obs, compare):
    for crit_meas, comp_meas inzip(crit_lst, comp_lst):
        print(crit_meas, comp_meas)
        if crit_meas.value != comp_meas.value: # example of comparing their valuesprint('Mis-Match', crit_meas.value, comp_meas.value)

Solution 2:

I do not know if you really need the two-dimensional structure of your Measurements, this turns this into a three-dimensional structure in numpy. If that is not necessary you can drop the extra dimension.

import numpy as np

lower = 20upper = 110

meas = np.array([[[100, 0.3], [33, 0.5]], [[150, 0.3], [35, 0.5]]])
crit = np.array([[999, 999]])
comp = np.array([[[100, 0.3], [33, 0.5]], [[150, 0.3], [35, 0.5]]])

mask = (meas[:,:,0] > lower) * (meas[:,:,0] < upper)

meas[mask,0] = (mask * crit)[mask] # apply mask to inner first column

out = (meas == comp).all(axis=2) # compare each measurement to respective one in comp

print(out)

This gives:

[[False False]
 [ True False]]

Post a Comment for "Using Yield Print Output"