Comparing Two 2-dimensional Lists
I was wondering how I would compare two 2-dim lists by their location. For instance, I have two 2-dim lists with 0's and 1's ,and I want to create a function that would return True
Solution 1:
any(
cell_1 and cell_2
for row_1, row_2 in zip(list_1, list_2)
for cell_1, cell_2 in zip(row_1, row_2)
)
Solution 2:
That looks fine to me, although you don't account for the two entries being 0 as well.
You might also want to use the actual length of the lists. So instead
for x in range(len(a)):
for y in range(len(a[x])):
...
That assumes the lists will have the same size.
Make sure that you also return false at the end of the function (since it wouldn't have gotten to that line if there were a collision)
Solution 3:
If collide(a,b)
should return True if all corresponding elements are the same, and False otherwise, then you'd want the if inside you nested loop to be:
if a[x][y] != b[x][y]:
return False
Then, after the outer loop, you can return True
(since by that point, you've checked all elements and none of them were a mismatch).
Post a Comment for "Comparing Two 2-dimensional Lists"