1 Star 0 Fork 0

我是妖怪/python学习笔记初级

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
adv_06.py 781 Bytes
一键复制 编辑 原始数据 按行查看 历史
我是妖怪 提交于 2020-10-29 16:41 +08:00 . 如何快速找到字典中的公共键
'''
python 高级笔记 如何快速找到字典中的公共键
来源:https://www.bilibili.com/video/BV1b5411s76z?p=5
'''
from functools import reduce
from random import randint, sample
# 生成随机字典
dict01 = {x: randint(0,100) for x in sample("abcdefghij",randint(6,9))}
dict02 = {x: randint(0,100) for x in sample("abcdefghij",randint(6,9))}
dict03 = {x: randint(0,100) for x in sample("abcdefghij",randint(6,9))}
print(dict01)
print(dict02)
print(dict03)
# 方案1 遍历
res = []
for x in dict01:
if x in dict02 and x in dict03:
res.append(x)
print(res)
# 方案2 集合交集
res = list(dict01.keys() & dict02.keys() & dict03.keys())
print(res)
# 方案3 map
res = reduce(lambda a, b: a & b, map(dict.keys, [dict01, dict02, dict03]))
print(res)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/theonee/python-learning.git
git@gitee.com:theonee/python-learning.git
theonee
python-learning
python学习笔记初级
master

搜索帮助