寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
‘’’ from random import randint, sample a1 = {k: randint(1, 4) for k in ‘abcdefg’} a2 = {k: randint(1, 4) for k in ‘abcdefg’} a3 = {k: randint(1, 4) for k in ‘abcdefg’} a4 = {k: randint(1, 4) for k in ‘abcdefg’} a = a1.keys() & a2.keys() & a3.keys() & a4.keys() print(a) a1.keys():得到a1字典的key,一set格式; a1.keys() & a2.keys() & a3.keys() & a4.keys():取4个集合的公共元素; a为一个集合(set)
方法三:使用map即reduce(用于求n个字典的公共key)
from random import randint, sample from functools import reduce a1 = {k: randint(1, 4) for k in ‘abcdefg’} a2 = {k: randint(1, 4) for k in ‘abcdefg’} a3 = {k: randint(1, 4) for k in ‘abcdefg’} a4 = {k: randint(1, 4) for k in ‘abcdefg’} b1 = map(dict.keys, [a1, a2, a3, a4]) b2 = reduce(lambda a ,b: a & b, b1) print(b2) b1 = map(dict.keys, [a1, a2, a3, a4]) #以集合形式取每个字典的keys;
感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:
① 2000多本Python电子书(主流和经典的书籍应该都有了)
② Python标准库资料(最全中文版)
③ 项目源码(四五十个有趣且经典的练手项目及源码)
④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)
⑤ Python学习路线图(告别不入流的学习)