Python - intersection between a list and keys of a dictionary

后端 未结 6 1577
耶瑟儿~
耶瑟儿~ 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:30

    You can use the following code:

    keys = set(l1).intersection(set(d1.keys()))
    result = {k:d1[k] for k in keys}
    

    EDIT: As commenters suggest you can replace the first line with, in Python 2.x:

    keys = set(l1).intersection(d1)
    

    And in Python 3.x:

    keys = d1.keys() & l1
    

提交回复
热议问题