I want to calculate absolute difference between all elements in a set of integers. I am trying to do abs(x-y)
where x
and y
are two elements in the set. I want to do that for all combinations and save the resulting list in a new set.
Asked
Active
Viewed 3,462 times
3

Lokesh Meher
- 437
- 4
- 15
-
4As python sets are unordered, there is no "last element". You could convert it to a list before you do your stuff. `list(yourset)` – MaxNoe Jun 12 '16 at 14:51
-
1in general you can just turn any set into a list as in `list(myset)` and then use your list algorithm. but maybe if you provide an example of what you're actually wanting to achieve there might be better approaches. – miraculixx Jun 12 '16 at 14:55
-
1python set is "Unordered collections of unique elements". So there is no meaning of "last element in a python set" – Eular Jun 12 '16 at 14:56
-
@miraculixx I want to calculate the absolute difference between all elements in a set of integers. – Lokesh Meher Jun 12 '16 at 15:00
-
If you want to get the difference of two python sets , you can simply use set `difference` method i:e : `a = {1,2,3,4} a.difference({3,4,5,6,7})` or you can simply do set comprehensions like follows `{x for x in {1,2,3,4} if x not in {3,4,5,6,7}}` – TMKasun Jun 12 '16 at 15:01
-
1@LokeshMeher Maybe you provide some sample input and expected output... – user2390182 Jun 12 '16 at 15:02
-
@TMKasun That's not what I meant. I mean something like `abs(x - y)` where `x` and `y` are two elements in the set. I want to do that for all elements and save the result list in a new set. – Lokesh Meher Jun 12 '16 at 15:04
5 Answers
2
I want to calculate absolute difference between all elements in a set of integers (...) and save the resulting list in a new set.
You can use itertools.combinations:
s = { 1, 4, 7, 9 }
{ abs(i - j) for i,j in combinations(s, 2) }
=>
set([8, 2, 3, 5, 6])
combinations
returns the r-length tuples of all combinations in s without replacement, i.e.:
list(combinations(s, 2))
=>
[(9, 4), (9, 1), (9, 7), (4, 1), (4, 7), (1, 7)]

miraculixx
- 10,034
- 2
- 41
- 60
-
Is there a faster way? It's taking too much time (> 10 secs) for bigger sets. – Lokesh Meher Jun 13 '16 at 07:23
0
As sets do not maintain order, you may use something like an ordered-set and iterate till last but one.

Siva Umapathy
- 177
- 3
0
For completeness, here's a solution based on Numpy ndarray
's and pdist():
In [69]: import numpy as np
In [70]: from scipy.spatial.distance import pdist
In [71]: s = {1, 4, 7, 9}
In [72]: set(pdist(np.array(list(s))[:, None], 'cityblock'))
Out[72]: {2.0, 3.0, 5.0, 6.0, 8.0}

Tonechas
- 13,398
- 16
- 46
- 80
0
Here is another solution based on numpy:
data = np.array([33,22,21,1,44,54])
minn = np.inf
index = np.array(range(data.shape[0]))
for i in range(data.shape[0]):
to_sub = (index[:i], index[i+1:])
temp = np.abs(data[i] - data[np.hstack(to_sub)])
min_temp = np.min(temp)
if min_temp < minn : minn = min_temp
print('Min difference is',minn)
Output: "Min difference is 1"

Alberto Lanaro
- 51
- 5
0
Here is another way using combinations:
from itertools import combinations
def find_differences(lst):
" Find all differences, min & max difference "
d = [abs(i - j) for i, j in combinations(set(lst), 2)]
return min(d), max(d), d
Test:
list_of_nums = [1, 9, 7, 13, 56, 5]
min_, max_, diff_ = find_differences(list_of_nums)
print(f'All differences: {diff_}\nMaximum difference: {max_}\nMinimum difference: {min_}')
Result:
All differences: [4, 6, 8, 12, 55, 2, 4, 8, 51, 2, 6, 49, 4, 47, 43]
Maximum difference: 55
Minimum difference: 2

Michael
- 657
- 4
- 29