Python - intersection between a list and keys of a dictionary

后端 未结 6 1576
耶瑟儿~
耶瑟儿~ 2021-01-04 00:33

I have a list that looks like this:

l1 = [\'200:200\', \'90:728\']

I have a dictionary that looks like this:

d1 = {\'200:20         


        
6条回答
  •  生来不讨喜
    2021-01-04 01:12

    Define efficient. Anyway here's what I would do. If it was too slow I'd probably move it to Cython.

    s1 = set(l1)
    s2 = set(d1.keys())
    s3 = s1 & s2
    # now you can access d1 using only keys in s3, or construct a new dict if you like
    d2 = dict([(k,d1[k]) for k in s3])
    

提交回复
热议问题