I have a list [a,b,c,d]
and I want to produce a list that gives me the absolute value of the substractions of every two elements.
[|a-b|, |a-c|, |a-d|, |b-c|, |b-d|, |c-d|]
Is there a function or a nice pythonic 1-liner to do this?
I have a list [a,b,c,d]
and I want to produce a list that gives me the absolute value of the substractions of every two elements.
[|a-b|, |a-c|, |a-d|, |b-c|, |b-d|, |c-d|]
Is there a function or a nice pythonic 1-liner to do this?
Have you tried something like this? This is a quick way to get all combinations in a list:
>>> x = [1, 4, 9, 16]
>>> [abs(x[i]-x[j]) for i in range(len(x)) for j in range(i+1, len(x))]
[3, 8, 15, 5, 12, 7]