Remove entries of a dictionary that are not in a set

后端 未结 3 970
误落风尘
误落风尘 2021-01-15 08:04

Given the following dictionary and set:

d = {1 : a, 2 : b, 3 : c, 4 : d, 5 : e }
s = set([1, 4])

I was wondering if it is possible to remov

3条回答
  •  长情又很酷
    2021-01-15 09:01

    Another solution:

    d = {1 : "a", 2 : "b", 3 : "c", 4 : "d", 5 : "e" }
    s = set([1, 4])
    r = { k:d[k] for k in ( d.viewkeys() - s ) }
    

提交回复
热议问题