Check if items in a list exist in dictionary

后端 未结 1 1931
情歌与酒
情歌与酒 2021-01-18 23:52

My question might be a little complicated to understand but here\'s actually the thing. I have a nested dictionary that looks like this:

dict_a = {\'one\': {         


        
相关标签:
1条回答
  • 2021-01-19 00:47

    Make list1 a set and use dictionary views, and a list comprehension:

    set1 = set(list1)
    newlist = [key for key, value in dict_a.iteritems() if value.viewkeys() & set1]
    

    In Python 3, use value.keys() and dict_a.items instead.

    This tests if there is a set intersection between the dictionary keys and the set of keys you are looking for (an efficient operation).

    Demo:

    >>> dict_a = {'one': {'bird':2, 'tree':6, 'sky':1, 'TOTAL':9},
    ...           'two': {'apple':3, 'sky':1, 'TOTAL':4},
    ...           'three': {'tree':6, 'TOTAL':6},
    ...           'four': {'nada':1, 'TOTAL':1},
    ...           'five': {'orange':2, 'bird':3, 'TOTAL':5}
    ...           }
    >>> set1 = {'bird','tree'}
    >>> [key for key, value in dict_a.iteritems() if value.viewkeys() & set1]
    ['three', 'five', 'one']
    

    Note that dictionary ordering is arbitrary (depending on the keys used and dictionary insertion and deletion history), so the output list order may differ.

    Technically speaking, you can use your list directly too (value.viewkeys() & list1 works) but making it a set states your intention more clearly.

    0 讨论(0)
提交回复
热议问题