python - 引用一个字典并制作一个新的字典
问题描述
我有一本字典如下。(dict_Xero_Profile)
dict_Xero_Profile ={'a': ['Demo'],
'b': ['PMS'],
'c' : ['All']}
我有一个清单如下。
x = ['Demo','PMS']
我需要通过从 dict_Xero_Profile 过滤列表元素来创建新的字典。
new_dict = {'a': ['Demo'], 'b': ['PMS']}
有人可以就此提出建议。并且还可以在我可以开始的地方练习这些基本的东西。(我可以练习简单问题的好地方)
解决方案
尝试以下
dict_Xero_Profile ={'a': ['Demo'],
'b': ['PMS'],
'c' : ['All'],}
x = ['Demo','PMS']
res = {k: l for k, l in dict_Xero_Profile.items() if ''.join(l) in x}
print(res)
输出
{'a': ['Demo'], 'b': ['PMS']}
推荐阅读
- mongodb - Mongodb: apply lookup on same collection and get sub-categories inside parent category array using a single query
- python - 如何在 amazon sagemaker 上运行自己的 python 代码
- javascript - 如何使用 Vuejs 3 中的渲染函数从子组件触发父组件的方法
- java - CFG 中的强制性规则序列
- c# - 当我在 WM_PARENTNOTIFY 处插入断点时,Winform Button.Click 未触发
- c# - 来自 SubList 或 SubList 属性的 LINQ NULL 异常
- python - 无法在 Python 3 中安装 bz2
- javascript - 如何使用 JavaScript 中的 Sql 查询为 Promise 函数制作模板?
- swift - 如何将类对象传递给函数但防止突变?
- ibm-cloud - 我无法在 IBM Visual Recognition 中选择精简计划