If you wanted to see if all element of one list appear in another list in that order (with possible extra elements in the second list), this would be an approach:
preds = [1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
labels = [1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1]
def contained_in_order(xs: list, ys: list) -> bool:
i = 0
try:
for x in xs:
i = ys.index(x, i) + 1
return True
except ValueError:
return False
print(contained_in_order(labels, preds))
This prints True
for the lists you provided as labels
is indeed contained in pred
in order.
Howevever, a list of 'yes'
and 'no'
values wouldn't make sense in this context, since there may be many ways to match. For example, consider:
preds = [1, 0, 1]
labels = [0, 0]
Clearly, labels
is not contained in preds
in order. But both [1, 0]
and [0, 1]
would be. So should the result be ['yes', 'no']
or ['no', 'yes']
?
You could write a function that generates all possible mismatches, but I doubt that's what you're after. Perhaps you can update the question to clarify what exactly you're trying to achieve, or comment on the answer.
Edit: If you're just after a list of matches, which you say you'd like to see as strings, this is all you need:
preds = [1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
labels = [1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1]
yes_no = ['yes' if p == l else 'no' for p, l in zip(preds, labels)]
print(yes_no)
Output:
['yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'no', 'no', 'yes']