I have a set of objects with attribute "coords" (a tuple of two integers) and a method returning this tuple. Additionally, I have a list of tuple coordinates. I want to add the objects whose coords attribute can be found in the list, to a new list.
First I tried this with a simple comprehension:
coords_list = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
objects_with_matching_coords = [i for i in objects if i.get_coords() in coords_list]
But it seems that get_coords is not satisfying the in condition, even though it does indeed return a tuple of two integers, exactly as in coords_list. The resulting list is empty.
I tried rewriting it as a for loop:
objects_with_matching_coords = []
for obj in objects:
if obj.get_coords() in coords_list :
objects_with_matching_coords.append(obj)
else:
continue
Same result, objects_with_matching_coords is empty. Does in not treat tuples in a list as items? What's the deal here?
Aucun commentaire:
Enregistrer un commentaire