How to find elements that are common to all lists in a nested list?

后端 未结 4 2036
孤城傲影
孤城傲影 2020-12-21 12:37

I have a large nested list and each list within the nested list contains a list of numbers that are formatted as floats. However every individual list in the nested list is

4条回答
  •  猫巷女王i
    2020-12-21 12:51

    Ashwini Chaudhary's solution is elegant, but could be quite inefficient for large inputs because it creates many intermediate sets. If your nested_list is large do this:

    >>> set.intersection(set(nested_list[0]), *itertools.islice(nested_list, 1, None))
    set([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0])
    

提交回复
热议问题