For an application I'm working on, I'm searching a directory of files, and expecting to find matching pairs of files to perform some further analysis on.
In this case, a pair is defined as matching on some subset of attributes, but differing in some other attributes.
As part of the error handling/warning, I want to identify any files found that are "incomparable," i.e. files for which the expected "partner" in the pair is not found.
I have a class of objects to store the structured attribute information, and when I read files in the directory, I store each file I find as an element in list of these objects.
Here's a silly simple example
class glove(object):
def __init__(self, size, color, is_right):
self.size = size
self.color = color
self.is_right = is_right
def __repr__(self):
if self.is_right:
hand = "right"
else:
hand = "left"
s = "{} {} {}".format(self.size, self.color, hand)
return(s)
gloves = [glove('med', 'black', False),
glove('med', 'black', True),
glove('lg', 'black', False),
glove('lg', 'black', True),
glove('med', 'brown', False),
glove('med', 'brown', True),
glove('lg', 'blue', False),
glove('med', 'tan', False)]
left_gloves = [x for x in gloves if not x.is_right]
right_gloves = [x for x in gloves if x.is_right]
Let's assume that there's no duplicate elements in the list, and let's define a "pair" as two glove
objects that have matching glove.size
and glove.color
but different values of glove.is_right
(i.e. one is Right and one is Left).
Now I'd like to identify incomplete pairs (perhaps into a list of leftovers
so that I could error or warn appropriately, e.g. "No Left lg blue glove found" "No Left med tan glove found."
I've seen answers that teach how to identify items "missing" from pairs of lists, but my application has a couple of complexities that I couldn't figure out how to address: linking on attributes of an object, and linking on multiple attributes of an object.
I imagine something is possible with for loops and list comprehension, but I can't quite figure out how to link it all together.