I'm trying to do a huge amount of simple "intersection" operations with integers. Unfortunately, I do not have numpy/scipy available in the setup, and I'm not able to change that.
I noticed on stackoverflow that the Python set operation nicely sorts the data, which not only speeds up loads of cases, but in my case, I'd actually like to sort the data as well, thus it would be an awesome bonus.
I'm now just afraid it does not always work, so I went to test:
import random one = range(100) two = range(50) three = range(50) for i in xrange(1000000): # shuffle the lists random.shuffle(one) random.shuffle(two) # do set operation res = [v for v in set(one) & set(two)] if res != three: print res
The result is that all the samples are sorted (no wrong cases are printed).
While this is quite convincing, I'd like to know if there would be a case where the integers are not completely sorted when using a set intersection?