Python: Fast extraction of intersections among all possible 2-combinations in a large number of lists

前端 未结 3 577
星月不相逢
星月不相逢 2021-02-06 17:10

I have a dataset of ca. 9K lists of variable length (1 to 100K elements). I need to calculate the length of the intersection of all possible 2-list combinations

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 17:46

    Try this:

    _lists = [[1, 2, 3, 7], [1, 3], [1, 2, 3], [1, 3, 4, 7]]
    _sets = map( set, _lists )
    _intersection = reduce( set.intersection, _sets )
    

    And to obtain the indexes:

    _idxs = [ map(_i.index, _intersection ) for _i in _lists ]
    

    Cheers,

    José María García

    PS: Sorry I misunderstood the question

提交回复
热议问题