小编Vin*_*ent的帖子

如何过滤dict以仅包含给定列表中的键?

这两者都是Python和stackoverflow的新手.感谢您的耐心和帮助.

我想根据列表的内容过滤一个字典,如下所示:

d={'d1':1, 'd2':2, 'd3':3}

f = ['d1', 'd3']

r = {items of d where the key is in f}
Run Code Online (Sandbox Code Playgroud)

这是荒谬的吗?如果没有,那么正确的语法是什么?

谢谢您的帮助.

文森特

python dictionary list filter

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

dictionary ×1

filter ×1

list ×1

python ×1