字典公共键

from functools import reduce
import random
s1 = {x: random.randint(1,4) for x in random.sample('abcdefg',random.randint(3,6))}#七名球员随机选取3-6个各进球随机1-4个
s2 = {x: random.randint(1,4) for x in random.sample('abcdefg',random.randint(3,6))}
s3 = {x: random.randint(1,4) for x in random.sample('abcdefg',random.randint(3,6))}
#方案1:
res=[]
for k in s1:
if k in s2 and k in s3:
res.append(k)
print(res)

#方案2:
allKeys = map(dict.keys,[s1,s2,s3])
sameK = reduce(lambda a,b:a & b,allKeys)
print(sameK)

输出:

 

posted @ 2019-10-05 17:35  Bambo0  阅读(185)  评论(0编辑  收藏  举报