python 字典取交加

python 字典取交集python 字典取交集方法: a{1: [a, b], 2: [d, e]} b{1: [a, f]

python 字典取交集

python 字典取交集


方法:

>>> a

{'1': ['a', 'b'], '2': ['d', 'e']}


>>> b

{'1': ['a', 'f'], '3': ['d', 'e']}


>>> dict([(i,b[i]) for i in filter(a.has_key,b.keys())])

{'1': ['a', 'f']}




更好的方法:(不过还没测试过效率是否更好,有空验证下)


>>> dict([(i,b[i]) for i in set(a).intersection(b)])

{'1': ['a', 'f']}