Issue
I have an array (fe: [1,2,3,4,5,...]. I want to compare each element of the array with every other element in the array. So I want to have:
1-2
1-3
1-4
1-5
2-3
2-4
2-5
3-4
3-5
4-5
Currently, this is my code
for el_a in my_array:
idx_a = np.where(my_array == el_a)[0][0]
for idx_b in range(idx_a+1, len(my_array)):
el_b = my_array[idx_b]
print(el_a,el_b)
I compare every element el_a (first for loop), with every element el_b which comes after el_a (second for loop).
The algoritm is working correctly, however, it is very slow. Is there someone with a better, more efficient solution?
Solution
This will provide the comparison result for all possible combinations as a list:
import numpy as np
from itertools import combinations
my_array = np.array([1, 2, 3, 4, 5])
print([item[0]==item[1] for item in combinations(my_array,2)])
Answered By - Pranta Palit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.