python - Find a value in a dictionary by searching using an inexact / not complete string -
hi i'm learning python , i'm facing problem dictionaries:
i made dictionary contains shows , number of seasons have
all_shows = {'modern family': 3 , 'how met mother': 9 , "modern world" : 12 }
and made possible user number of season searching name of show
showname = input('<<enter show: >>') season = (all_shows.get(showname)) print (season)
the problem number of season returned if user writes exact name of show. i'm trying fix if user write "modern" shows "modern" in title ( if write in lower case) , can select show 1 wants.
i looked online , found fuzzywuzzy. think me achieving want? thought using similar show title 1 selected, if wrote " how met mother " result still " 9 " , if wrote " modern " list follow select shows contains "modern" 1 wants.
is fuzzywuzzy looking or there other ways that?
regular expressions friends.
import re all_shows = {'modern family': 3, 'how met mother': 9, "modern world": 12} input = 'modern' rs = {x: y x, y in all_shows.items() if re.match('.*%s.*' % input, x, re.ignorecase)} print(rs)
output:
{'modern world': 12, 'modern family': 3}
if user inputs odern
output still 2 shows modern xxxx name
Comments
Post a Comment