Find which dictionaries from a list contain word

跟風遠走 提交于 2019-12-25 01:09:34

问题


I have a dictionary with each keys having multiple values in a list. The tasks are:

  1. To detect whether a given word is in the dictionary values
  2. If it is true, then return the respective key from the dictionary

Task 1 is achieved by using an if condition:

if (word in dictionary[topics] for topics in dictionary.keys())

I want to get the topics when the if condition evaluates to be True. Something like

if (word in dictionary[topics] for topics in dictionary.keys()):
   print topics

回答1:


You can use a list comprehension (which is like a compressed for loop). They are simpler to write and can in some circumstances be faster to compute:

topiclist = [topic for topic in dictionary if word in dictionary[topic]]

You don't need dictionary.keys() because a dict is already an iterable object; iterating over it will yield the keys anyway, and (in Python 2) in a more efficient way than dictionary.keys().

EDIT: Here is another way to approach this (it avoids an extra dictionary look up):

topiclist = [topic for (topic, tlist) in dictionary.items() if word in tlist]

Avoiding the extra dictionary lookup may make it faster, although I haven't tested it.

In Python 2, for efficiency sake, you may want to do:

topiclist = [topic for (topic, tlist) in dictionary.iteritems() if word in tlist]



回答2:


if (word in dictionary[topics] for topics in dictionary.keys())

the problem with the above line is that you are creating a generator object that assesses whether word is in each value of dictionary and returning a bool for each. Since non-empty lists are always true, this if statement will ALWAYS be true, regardless if the word is in the values or not. you can do 2 things:

  • using any() will make your if statement work:

    if any(word in dictionary[topics] for topics in dictionary.keys()):
    

however, this does not solve your initial problem of capturing the key value. so instead:

  • use an actual list comprehension that uses the predefined (I assume) variable word as a filter of sorts:

    keys = [topics for topics in dictionary if word in dictionary[topics]]
    

or

  • use filter()

    keys = filter(lambda key: word in dictionary[key],dictionary)
    

these both do the same thing. reminder that iterating through dictionary and dictionary.keys() are equivalent

just a note that both these methods return a list of all the keys that have values containing word. Access each key with regular list item getting.




回答3:


It sounds like the word you are searching for will be found in only one key. Correct?

If so, you can just iterate over the dictionary's key-value pairs until you find the key that contains the search word.

For Python 2:

found = False
for (topic, value) in dictionary.iteritems():
    if word in topic:
        found = True
        print topic
        break

For Python 3, just replace iteritems() with items().



来源:https://stackoverflow.com/questions/34004185/find-which-dictionaries-from-a-list-contain-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!