Reputation: 55
I have an array that has unique pairs of integers that is fully sorted like this:
x = np.array([[40, 202], [44, 117], [44, 125], [45, 53], [45, 675], [45, 1013], [53, 675], [116, 845], [117, 125], [126, 130], [130, 144]])
How can I obtain values of a, b, c that occur in three different tuples [a, b] and [a, c] and [b, c]? For example, [44, 117] and [44, 125] and [117, 125] fulfill this pattern with a = 44, b = 117, and c = 125.
Looking at it vertically is perhaps an easier way to visualize what I’m looking for. T_1, T_2, and T_3 refer to individual tuples in array x.
T_1 = [a, b] eg [44, 117]
T_2 = [a, c] eg [44, 125]
T_3 = [b, c] eg [117, 125]
Simulated code and output would be:
def findTriple(arr):
"""Returns a 2darray with tuple a, b, c for each match that
results in the union [a, b] and [a, c] and [b, c] in array x"""
# do something
return res
data = findTriple(x)
print (data) #two answers for example x given.
[[44, 117, 125], [45, 53, 675]]
I couldn't figure out a way to create a mask or use list comprehension.
Upvotes: 1
Views: 41
Reputation: 6652
You can accomplish it with list comprehension
>> [(x1, x2, xx2) for x1, x2 in x for xx1, xx2 in x if x2 == xx1]
[(44, 117, 125), (45, 53, 675), (126, 130, 144)]
Upvotes: 1