python intersect of dict items

こ雲淡風輕ζ 提交于 2019-12-24 02:56:22

问题


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


回答1:


from functools import reduce # if Python 3

reduce(lambda x, y: x.intersection(y), (set(x.split(',')) for x in aDict.values()))



回答2:


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)



回答3:


Python Sets 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



回答4:


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



回答5:


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 ",".



来源:https://stackoverflow.com/questions/7765583/python-intersect-of-dict-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!