标签:red nbsp reduce 如何 随机 [] 统计 pre pen
比如一场球赛每轮球员进球统计
比如
第一轮 {"李小习":2,“姣明”:3,“李总”:0}
第二轮{“李小习“:1,”绞明‘:2,"李总‘:3}
第三轮{"李总":0,“姣明”:1,“李小习”:2}
。。。。。。
统计出前几轮,每场比赛都有进球的球员
原始方法一
from random import randint,sample
#随机产生球员进球
#samle("abcdef",3)
#samle(‘abcdef‘,randint(3,6))
s1 = {x:randint(1,4)for x in sample("abcdegv",randint(3,6))}
s2 = {x:randint(1,4)for x in sample("abcdegv",randint(3,6))}
s3 = {x:randint(1,4)for x in sample("abcdegv",randint(3,6))}
res = []
for k in s1:
if k in s2 and k in s3:
res.append(k)
print(res)
#方法二,用集合操作
print(s1.keys()&s2.keys()&s3.keys())
方法三
from functools import reduce
dd = map(dict.keys,[s1,s2,s3])
i = reduce(lambda a,b:a&b,map(dict.keys,[s1,s2,s3]))
print(i)
标签:red nbsp reduce 如何 随机 [] 统计 pre pen
原文地址:https://www.cnblogs.com/fgxwan/p/9575769.html