简体   繁体   中英

Find a value in a dictionary by searching using an inexact / not complete string

Hi I'm learning python and I'm facing a problem with Dictionaries:

I made this Dictionary that contains shows and the number of seasons they have

all_shows = {'Modern family': 3 , 'How I Met Your Mother': 9 , "Modern World" : 12 }

and I made possible to the user to get the number of the season by searching for the name of a show

showname = input('<<Enter a show: >>')
season = (all_shows.get(showname))

print (season)

The problem is that the number of season is returned only if the user writes the exact name of the show. I'm trying to fix this so even if the user write something like "Modern" he will get all the shows with "modern" in the title ( even if he write it all in lower case) and he can select which show is the one he wants.

I looked online and found FuzzyWuzzy. Do you think that it will help me achieving what I want? I thought that by using it the most similar show title would be the one selected, so if I wrote " how met mother " the result would still be " 9 " and if he wrote " Modern " a list would follow where he could select which shows that contains "modern" is the one he wants.

Is fuzzywuzzy what I am looking for or there are other ways to do that?

I have personally always used fuzzywuzzy but this is built of an in-built Python module called difflib which you may want to look into. In my opinion, fuzzywuzzy is simpler to use and most likely better for your needs.

The following code (pure python, without imports, hence simple, straightforward and fast):

[(x, all_shows.get(x)) for x in all_shows.iterkeys() if "Modern".lower() in x.lower()]

is a list comprehension returning a list of tupels for each entry in the dictionary that has "modern" (case in-sensitive) in its key at any position in the String.

For your example dictionary it returns:

[('Modern World', 12), ('Modern family', 3)]

You could substitute "Modern".lower() with some variable, like search_string.lower() .

Regular expressions are your friends.

import re

all_shows = {'Modern family': 3, 'How I Met Your Mother': 9, "Modern World": 12}

input = 'modern'

rs = {x: y for x, y  in all_shows.items() if re.match('.*%s.*' % input, x, re.IGNORECASE)}

print(rs)

Output:

{'Modern World': 12, 'Modern family': 3}

If the user inputs odern the output is still the two shows with M odern XXXX as name

You can use edit distance . Compare all keys of dictionary with your input and store the key which has least distance from input, then get the value of that key and return.

import editdistance
import sys
all_shows = {'Modern family': 3 , 'How I Met Your Mother': 9 , "Modern World" : 12 }
def get_seasons(input_str):
    min_dist_key = None
    dist = sys.maxint
    for key in all_shows.keys():
        this_dist = editdistance.eval(key, input_str)
        if this_dist < dist:
            dist = this_dist
            min_dist_key = key
    return all_shows.get(min_dist_key)

This code might not be working, but I hope you got the idea.

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