2.5 如何快速找到多个字典中的公共键

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'zwq'
__data__ = '2017/2/16 11:46'

from random import randint,sample
li1  = sample('abcdefg',randint(3,6)) # 随机取样
s1 = {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
s2 = {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
s3 = {x:randint(1,4) for x in sample('abcdefg',randint(3,6))}
print (s1)
print (s2)
print (s3)

#1 使用集合的viewkeys
s1.viewkeys()
s2.viewkeys()
s3.viewkeys()
# 2 去集合的交集
s1.viewkeys()&s2.viewkeys()&s3.viewkeys()

# 使用map,reduce函数
map(dict.viewkeys,[s1,s2,s3])
reduce(lambda a,b:a&b,map(dict.viewkeys,[s1,s2,s3]))

 

posted @ 2017-02-16 15:41  邹文强  阅读(94)  评论(0编辑  收藏  举报