How to find common elements in list of lists?

后端 未结 7 1203
無奈伤痛
無奈伤痛 2020-11-27 18:13

I\'m trying to figure out how to compare an n number of lists to find the common elements. For example:

p=[ [1,2,3],
    [1,9,9],
      ..
      ..
    [1,2         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 18:56

    You are looking for the set intersection of all the sublists, and the data type you should use for set operations is a set:

    result = set(p[0])
    for s in p[1:]:
        result.intersection_update(s)
    print result
    

提交回复
热议问题