Say I have an array/list of things I want to compare. In languages I am more familiar with, I would do something like
for (int i = 0, i < mylist.size(); i++)
for (int j = i + 1, j < mylist.size(); j++)
compare(mylist[i], mylist[j])
This ensures we only compare each pair once. For some context, I am doing collision detection on a bunch of objects contained in the list. For each collision detected, a small 'collision' object describing the collision is appended to a list, which another routine then loops through resolving each collision (depending on the nature of the two colliding objects). Obviously, I only want to report each collision once.
Now, what is the pythonic way of doing this, since Python favors using iterators rather than looping over indices?
I had the following (buggy) code:
for this in mylist:
for that in mylist:
compare(this, that)
But this clearly picks up each collision twice, which lead to some strange behavior when trying to resolve them. So what is the pythonic solution here?