Search code examples
pythonlistcoordinate

python - Common lists among lists in a list


I need to be able to find the first common list (which is a list of coordinates in this case) between a variable amount of lists.

i.e. this list

>>> [[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]]

should return

>>> [3,4]

If easier, I can work with a list of all common lists(coordinates) between the lists that contain the coordinates.

I can't use sets or dictionaries because lists are not hashable(i think?).


Solution

  • Correct, list objects are not hashable because they are mutable. tuple objects are hashable (provided that all their elements are hashable). Since your innermost lists are all just integers, that provides a wonderful opportunity to work around the non-hashableness of lists:

    >>> lists = [[[1,2],[3,4],[6,7]],[[3,4],[5,9],[8,3],[4,2]],[[3,4],[9,9]]]
    >>> sets = [set(tuple(x) for x in y) for y in lists]
    >>> set.intersection(*sets)
    set([(3, 4)])
    

    Here I give you a set which contains tuples of the coordinates which are present in all the sublists. To get a list of list like you started with:

    [list(x) for x in set.intersection(*sets)]
    

    does the trick.

    To address the concern by @wim, if you really want a reference to the first element in the intersection (where first is defined by being first in lists[0]), the easiest way is probably like this:

    #... Stuff as before
    intersection = set.intersection(*sets)
    reference_to_first = next( (x for x in lists[0] if tuple(x) in intersection), None ) 
    

    This will return None if the intersection is empty.