Python 2.7
data structure is like this,
hitdict = {'A':[1,2,3,4], 'B':[2,3,6,5], 'C':[2,3,7,8]}
Note the number of key:value pairs is variable. I want the intersection of A, B, C :
[2,3]
For intersection of two or more list, most answers suggest either set class or lambda functions. eg. intersection of lists of list, using lambda functions
and from a previous question, where a,b,c
are lists
>> set(a) & set(b) & set(c)
I have a number of dictionaries like this with variable number of lists.
My main confusion is about how to use the above set(), since
if I use for loop with hitdict.items()
it will give k:v pairs only one at a time. Hence the code below is incorrect.
for (k,v) in hitdict.items():
common = set(hitdict[k]) + set(hitdict[k+1])
How to do this in Python ?