简体   繁体   中英

Match dictionary with list (Python)

I have a dictionary of words with categories. I want to output the categories if the words match the words in my list. This is what my code looks like at the moment:

dictionary = {
    "object" : 'hat',
    "animal" : 'cat',
    "human" : 'steve'
}

list_of_things = ["steve", "tom", "cat"]

for categories,things in dictionary.iteritems():
    for stuff in list_of_things:

        if stuff in things:
            print categories
        if stuff not in things:
            print "dump as usual"

Currently the output looks like this:

dump as usual
dump as usual
dump as usual
human
dump as usual
dump as usual
dump as usual
dump as usual
animal

But I want the output to look like this:

human
dump as usual 
animal

I don't want my list to print everything it iterates through in the dictionary. I only want it to print terms that match. How would I do this?

You could use a boolean inside the inner for loop, which changes from False (category not found) to True (category was found), and then only print the categories at the end of the for loop if boolean = False:

isMatchFound = False
for categories, things in dictionary.iteritems():
    isMatchFound = False
    for stuff in list_of_things:
        if stuff in things:
            isMatchFound = True
            print stuff
    if isMatchFound == False:
        print("dump as usual")

Depending on how much your actual data look like your example, you could do

category_thing= {
    "object" : 'hat',
    "animal" : 'cat',
    "human" : 'steve'
}

list_of_things = ["steve", "tom", "cat"]
thingset=set(list_of_things) # just for speedup

for category,thing in category_thing.iteritems():

        if thing in thingset:
            print category
        else:
            print "dump as usual"

or if your mapping really is as simple as in your example you can do

category_thing= {
    "object" : 'hat',
    "animal" : 'cat',
    "human" : 'steve'
}

thing_category=dict((t,c) for c,t in category_thing.items()) # reverse the dict - if you have duplicate values (things), you should not do this

list_of_things = ["steve", "tom", "cat"]

for stuff in list_of_things:
     msg=thing_category.get(stuff,"dump as usual")
     print msg

First of all, your dictionary is poorly structured; it seems to have its keys and values swapped. By using the categories as keys, you can only have one object per category, which is probably not what you want. It also means that you have to read every entry in the dictionary in order to look up an item, which is generally a bad sign. The fix for this is simple: put the item on the left of the colon, and the category on the right. You can then use the 'in' operator to easily search the dictionary.

As far as the question you are directly asking, you should be looping over the list_of_things first, checking each against the dictionary, then printing the result. This will print exactly one thing per item in the list.

dictionary = {
    'hat' : 'object',
    'cat' : 'animal',
    'steve' : 'human'
}

list_of_things = ['steve', 'tom', 'cat']

for thing in list_of_things:
    if thing in dictionary:
        print dictionary[thing]
    else:
        print "dump as usual"

This outputs:

human
dump as usual
animal

Since you want only 3 lines in output, your for-loops should be re-ordered.

for stuff in list_of_things:
  print_val = None
  for categories,things in dictionary.iteritems():
    if stuff in things:
      print_val=categories
  if print_val is None:
    print_val="dump as usual"
  print print_val

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM