Search Python dictionary where value is list

后端 未结 2 692
走了就别回头了
走了就别回头了 2020-12-17 04:46

If I had a dictionary where the value was set to a list by default, how could I go about searching all of these lists in the dictionary for a certain term?

For Examp

相关标签:
2条回答
  • 2020-12-17 05:32
    [k for k, v in textbooks.iteritems() if 'red' in v]
    

    It is Pythonic shorthand for

    res = []
    for key, val in textbooks.iteritems():
        if 'red' in val:
            res.append(key)
    

    See list comprehension in Python documentation

    0 讨论(0)
  • 2020-12-17 05:47
    [key for key, corresponding_list in textbook.items() if 'red' in corresponding_list]
    
    0 讨论(0)
提交回复
热议问题