Search code examples
pythonnlptuplesintersectioncollocation

Looking for an elegant way of finding the intersection between two lists of words' tuples in different orders


I think its best to show an example of what I'm trying to do. The point is that I'm looking for an elegant way of doing so.

Let's say I have two lists of tuples:

x = [('a', 'c', 'e'), ('k', 'n')]
y = [('e', 'd', 'w'), ('c', 'a', 'e'), ('n', 'k')]
z = set(x).intersection(y)

If I compute the intersection between x and y I get an empty set.

My goal is to find out that there are two elements in y that are identical to the two elements in x while I don't care about the different order of the tuples. In addition I want to get the index of the matching element of the list y.

For me x[0] and y[1] are the same (again, don't care about the order) and the index I want to get is 1, same for x[1] and y[2] , both are the same for me and it should return the index 2 in this case.

Any ideas on how to do this in an elegant way?


Solution

  • Can't you make your x and y sets? E.g.:

    x = [('a', 'c', 'e'), ('k', 'n')]
    y = [('e', 'd', 'w'), ('c', 'a', 'e'), ('n', 'k')]
    set_x = set(frozenset(a) for a in x)
    set_y = [frozenset(a) for a in y]
    [y_element in set_x for y_element in set_y]
    

    returns: [False, True, True]

    If you want the actual indices:

    [i for i, y_element in enumerate(set_y) if y_element in set_x]
    

    returns: [1, 2]