📜  只保留 dict 中的几个键值 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:52.220000             🧑  作者: Mango

代码示例1
>>> dict_filter = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
>>> large_dict = {"a":1,"b":2,"c":3,"d":4}
>>> new_dict_keys = ("c","d")
>>> small_dict=dict_filter(large_dict, new_dict_keys)
>>> print(small_dict)
{'c': 3, 'd': 4}
>>>