Search code examples
pythonpython-3.xdata-structurespython-itertools

Get the intersection from a single nested list?


I have the following nested list:

list_ = [['The'], ['The', 'fox', 'quick'], ['quick', 'the'], ['dog']]

How can I compute efficiently the intersection between each sublist?:

['the', 'quick']

I tried to:

list(itertools.product([for e in [list_]]))

Solution

  • First, preprocess your list to make all words lower-case:

    list_ = [set(word.lower() for word in item) for item in list_]
    

    Then you want to use itertools.combinations and the set() operations:

    results = [x&y for x,y in itertools.combinations(list_,2)]
    # [{'the'}, {'the'}, set(), {'the', 'quick'}, set(), set()]