zoukankan      html  css  js  c++  java
  • 【Python—字典的用法】找到多个字典的公共键

    有 a,b,c,d,e,f 6名球员,他们在三轮比赛中的进球数用 s1,s2,s3 3个字典表示,找到每轮都有进球的球员?

    创建 s1,s2,s3 3个字典素材

    from random import randint,sample 
    
    s = 'abcdef'
    s1 = {x: randint(3,4) for x in sample(s,randint(4,6))}
    s2 = {x: randint(3,4) for x in sample(s,randint(4,6))}
    s3 = {x: randint(3,4) for x in sample(s,randint(4,6))}
    print(s1)
    print(s2)
    print(s3)
    输出:
    {'d': 4, 'a': 3, 'e': 3, 'f': 4} {'c': 4, 'b': 3, 'e': 3, 'f': 4} {'e': 3, 'c': 4, 'd': 3, 'a': 3}
    结果随机产生

    知识点:

    1. random.randint(a,b)随机生一个整数int类型,可以指定这个整数的范围。
    2. random.sample(sequence, k),从指定序列sequence中随机获取k个指定长度的片断。sample函数不会修改原有序列。

    方法一:通过遍历加if条件

    common_key = [i for i in s1 if i in s2 and i in s3]
    print(common_key)
    输出:
    ['e']

    知识点:

    列表生成式,上面一行语句等同于下面循环生成上面的list

    common_key = []
    for i in s1:
        if i in s2 and i in s3:
            coommon_key.append(i)
    print(common_key)
    输出:
    ['e']

    方法二:用集合set的交集、并集

    common_key = s1.keys()&s2.keys()&s3.keys()
    print(common_key)
    输出:
    {'e'}

    知识点:

    字典中 .keys(), .values() and .items() 3个方法,其中.keys(), .items() 返回的是一个set-like 对象,所以set具备的集合计算也可以用

    help(dict.keys)
    help(dict.values)
    help(dict.items)
    
    print(s1.keys())  #返回一个字典所有的键
    print(s1.keys()|s2.keys()|s3.keys()) # 并集
    print(s1.keys()&s2.keys()&s3.keys()) # 交集
    
    print(s1.items()) #返回一个字典所有的键值对,键值对的交集要求键和值都一样
    print(s1.items()&s2.items()&s3.items()) #交集
    输出:
    Help on method_descriptor:
    keys(...)
        D.keys() -> a set-like object providing a view on D's keys
    
    Help on method_descriptor:
    values(...)
        D.values() -> an object providing a view on D's values
    Help on method_descriptor: items(...) D.items() -> a set-like object providing a view on D's items
    dict_keys(['d', 'a', 'e', 'f'])
    {'e', 'd', 'a', 'f', 'c', 'b'}
    {'e'}
    dict_items([('d', 4), ('a', 3), ('e', 3), ('f', 4)])
    {('e', 3)}

    方法三:用 map() 函数 reduce() 函数, 适用于n个字典

    #第一步用 map 函数返回所有字典所有的键
    
    s = [s1,s2,s3]
    r = map(lambda x:x.keys(),s)
    r1 = map(dict.keys,s)  # r 和 r1 是同等效果,所以dict.keys是函数?  
    print(list(r1))
    
    #第二步用 reduce 函数算出前一项与后一项的交集 from functools import reduce print(reduce(lambda a,b:a&b,r))
    输出:
    [dict_keys(['d', 'a', 'e', 'f']), dict_keys(['c', 'b', 'e', 'f']), dict_keys(['e', 'c', 'd', 'a'])]
    {'e'}

    知识点:

    map 函数接收两个参数,一个是函数,一个是 Iterable,map 将传入的函数依次作用到序列的每个元素,并把结果作为新的 Iterator 返回。由于结果r是一个 Iterator,Iterator 是惰性序列,因此通过list()函数让它把整个序列都计算出来并返回一个list。

    reduce 把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce 把结果继续和序列的下一个元素做累积计算

    reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

     注意:

    map 返回的是 Iterator 惰性序列!

    print(reduce(lambda a,b:a&b,r1)) 
    输出:
    TypeError: reduce() of empty sequence with no initial value

    报错的原因:

    print(list(r1)) #因为前面print把r1这个 Iterator 序列全部打出来,r1里面就是空的序列,所以这上面填r1就会报错
    输出:
    []

    遗留疑问:

    map()函数第一个参数接收函数,所以 dict.keys() 是函数?这个不是方法吗?
    map(dict.keys,s)  
  • 相关阅读:
    网站初学笔记1
    如何在新项目中使用曾经创建的用户控件
    C#用户控件的一些尝试
    MVC的含义
    Chrome浏览器面板基础了解
    VScode快捷键
    Windows中的键盘快捷方式
    node.js状态码
    JavaScript小结
    Truncate a string
  • 原文地址:https://www.cnblogs.com/xujiu/p/8352186.html
Copyright © 2011-2022 走看看