首页 > 其他 > 详细

字典公共键

时间:2019-10-05 18:49:14      阅读:79      评论:0      收藏:0      [点我收藏+]
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)

输出:

技术分享图片

 

字典公共键

原文:https://www.cnblogs.com/cxxBoo/p/11625229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!