首页 > 解决方案 > 引用一个字典并制作一个新的字典

问题描述

我有一本字典如下。(dict_Xero_Profile)

dict_Xero_Profile ={'a': ['Demo'], 
                    'b': ['PMS'],
                    'c' : ['All']}

我有一个清单如下。

x = ['Demo','PMS']

我需要通过从 dict_Xero_Profile 过滤列表元素来创建新的字典。

new_dict = {'a': ['Demo'], 'b': ['PMS']}

有人可以就此提出建议。并且还可以在我可以开始的地方练习这些基本的东西。(我可以练习简单问题的好地方)

标签: pythonlistdictionary

解决方案


尝试以下

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']}

推荐阅读