码迷,mamicode.com
首页 > 编程语言 > 详细

python_如何快速找打字典中公共key

时间:2017-07-26 00:03:37      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:随机数   问题   []   bcd   usr   接收   返回   ros   场景   

场景实例:

       西班牙足球联赛,每轮球员进球统计:

       第一轮:{’1’:1,‘2’:4,‘5’:2,‘7’:3}

  第一轮:{’2’:1,‘5’:4,‘6’:2,‘3’:3}

       第一轮:{’1’:1,‘4’:4,‘6’:2,‘7’:3}

       ……

       问题:统计前N轮,都进球的球员?

如何解决这个问题呢?

       方法1:

#!/usr/bin/python3

from random import randint, sample


def get_s(player):
    # 获得三个赛季的随机记录
    s1 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s2 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s3 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    return s1, s2, s3


def public_keys(s1, s2, s3):
    # 定义中间列表
    median = []
    
    # 判断字典公共key,在s1,在s2,在s3肯定是公共key
    for key in s1:
        if key in s2 and key in s3:
            median.append(key)
    return median

if __name__ == ‘__main__‘:
    # 生成abcdef个球员
    player = ‘abcdef‘
    
    # 获得3个赛季的随机数
    s1, s2, s3 = get_s(player)
    
    # 获取3个赛季都进球的球员
    result = public_keys(s1,s2,s3)
    print(result)

       方法2:

              进球的球员都是数据中的key,通过求出每一轮的keys,然后做个交集就可以求出结果

#!/usr/bin/python3

from random import randint, sample


def get_s(player):
    # 获得三个赛季的随机记录
    s1 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s2 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s3 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    return s1, s2, s3


def public_keys(s1, s2, s3):
    # 返回各字典的单边值,并求出交集,即是结果
    return s1.keys() & s2.keys() & s3.keys()

if __name__ == ‘__main__‘:
    # 生成abcdef个球员
    player = ‘abcdef‘
    
    # 获得3个赛季的随机数
    s1, s2, s3 = get_s(player)
    
    # 获取3个赛季都进球的球员
    result = public_keys(s1, s2, s3)
    print(result)

  方法3:

         当N轮时候,通过map和reduce函数

#!/usr/bin/python3

from random import randint, sample
from functools import reduce


def get_s(player):
    # 获得三个赛季的随机记录
    s1 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s2 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    s3 = {k: randint(1, 6) for k in sample(player, randint(3, 6))}
    return s1, s2, s3


def public_keys(s1, s2, s3):
    result = reduce(lambda a, b: a & b, map(dict.keys, [s1, s2, s3]))
    # map(dict. keys,[s1,s2,s3,......N] 获得每轮的keys集合,s1……N为字典对象
    # reduce((lamda a,b: a&b,map())     把map获得的集合,依次放入a,b,返回交集
    # a,b = a&b, map中取出的一个值map(dict.keys, [s1, s2, s3])取得新的列表
    # reduce中的lambda只能传入两个参数
    # map 为高阶函数,接收一个map(函数, [参数]),迭代参数列表,一个传入函数中处理,返回处理结果变成新的列表
    
    return result

if __name__ == ‘__main__‘:
    # 生成abcdef个球员
    player = ‘abcdef‘
    
    # 获得3个赛季的随机数
    s1, s2, s3 = get_s(player)
    
    # 获取3个赛季都进球的球员
    result = public_keys(s1, s2, s3)
    print(result)

 

 

python_如何快速找打字典中公共key

标签:随机数   问题   []   bcd   usr   接收   返回   ros   场景   

原文地址:http://www.cnblogs.com/2bjiujiu/p/7236962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!