I'm trying to find the differences between an element of a list and all elements of a list for each element of that list. I think that's how I'd describe it? Let's use an example instead:
idx = [1, 4, 8, 10, 22]
Based on this question, it would be simple enough to just zip the elements together, but that would result in only one pairwise comparison per element, whereas I need to make multiple pairwise comparisons for each element.
My approach was to do the following:
diffs = [abs(i-j) for i in idx for j in idx]
But this is incredibly inefficient. What is the most efficient way to calculate the differences for all of the following tuples?
comparisons = [(1,4), (1,8), (1,10), (1, 22),
(4,8), (4,10), (4,22),
(8,10, 8,22)]
NOTE:
I would prefer answers using base Python 2.7, but would also like to know how to do this with Numpy, as I'm sure that package makes it easier.