list_of_list=[[1,2,3,4,133,90],[4,11,22,99,3],[5,3,22,66,12,10],[1,99,101]]
the code should return elements that can be common with one or multiple sublists.
output: [1,3,4,22,99]
a = [1, 2, 3, 4]
b = [2, 3, 4, 5, 6]
c = [3, 4, 5, 6, 10, 12]
elements_in_all = list(set.union(*map(set, [a, b, c])))
elements_in_all
doing mapping and set intersection which only picks common elements if the elements appeared in all sublists.
But in my case, the element can be common in one single sublist or in all sublists.
c = collections.Counter()
for sub in list_of_list:
c.update(set(sub))
this creates a mapping element => number of sublists
, from which you can pick whatever you want, for example:
[v for v, cnt in c.items() if cnt > 1]
to make a list of elements that occur in at least two lists.