python intersect of dict items
Question
ok another python question...
I have a dict:
aDict[1] = '3,4,5,6,7,8'
aDict[5] = '5,6,7,8,9,10,11,12'
aDict[n] = '5,6,77,88'
n could be any limit but the keys are not in sequence.
How do I extract the intersect of the csv'd items? So in this case the answer would be '5,6'.
Thanks
Solution
from functools import reduce # if Python 3
reduce(lambda x, y: x.intersection(y), (set(x.split(',')) for x in aDict.values()))
OTHER TIPS
First of all, you need to convert these to real lists.
l1 = '3,4,5,6,7,8'.split(',')
Then you can use sets to do the intersection.
result = set(l1) & set(l2) & set(l3)
Python Set
s are ideal for that task. Consider the following (pseudo code):
intersections = None
for value in aDict.values():
temp = set([int(num) for num in value.split(",")])
if intersections is None:
intersections = temp
else:
intersections = intersections.intersection(temp)
print intersections
result = None
for csv_list in aDict.values():
aList = csv_list.split(',')
if result is None:
result = set(aList)
else:
result = result & set(aList)
print result
Since set.intersection()
accepts any number of sets, you can make do without any use of reduce()
:
set.intersection(*(set(v.split(",")) for v in aDict.values()))
Note that this version won't work for an empty aDict
.
If you are using Python 3, and your dictionary values are bytes
objects rather than strings, just split at b","
instead of ","
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow