1

I wanted to ask this question. I've been trying to code a program that does this efficiently for ages but have a hard time getting anywhere. Essentially I have a string stored in a dictionary as such:

dic = {"a" : "Blue Jacket with buttons", "b" : "Green Jacket with a buttons",
      "c" : "Blue jacket"}

Lets say i want to find "b" but the user doesn't know exactly how it is stored in the dictionary so they enter keywords they wish to use to find the item.

keywords = "Blue, Jacket, Buttons"
keywords.split(",")

How would I use the keywords to find "b" in the dictionary? I tried doing an if statement but I can't get it to notice the difference between "a" and "c". How would I use keywords to find the items in the dictionary?

Thanks!

Fader910
  • 71
  • 1
  • 4
  • 3
    This isn't the best use for a dictionary :( You're performing a search on the dictionary values, you will need to iterate over each one. Maybe even *gasp* using regex. – cs95 Feb 28 '18 at 08:45
  • Possible duplicate of [Get key by value in dictionary](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – el_Rinaldo Feb 28 '18 at 08:54
  • @cᴏʟᴅsᴘᴇᴇᴅ I understand but I plan on pulling from a JSON which will be an issue for me :( – Fader910 Feb 28 '18 at 08:58

4 Answers4

1

This is my try, using set function. We only select the key if all keywords can be found in the corresponding value.

keywords = set([x.strip() for x in "Blue, Jacket, Buttons".lower().split(",")])
print([key for key, val in dic.items() if keywords <= set(val.lower().split())])
pe-perry
  • 2,591
  • 2
  • 22
  • 33
  • I like your answer, using set operator is more efficient. – Menglong Li Feb 28 '18 at 08:55
  • Hi, the JSON I am using has numbers as well as the "name" tag that I am using. It has a "name" element and also an "id" element that is made up of numbers and there is one for every item in the dictionary. How would I do it in that scenario? – Fader910 Feb 28 '18 at 09:10
  • @kitman0804 Hi, sorry for the late reply been busy with work. the JSON is too long to be posted here so I put it on pastebin (you can't do code snippets in comments on SO). https://pastebin.com/7TbJNdiK. I want to extract the "name" and the "id" from the json. – Fader910 Mar 07 '18 at 09:57
  • @AbcDef I'm not sure if it is better to create a new question for it (Maybe there are some similar questions in SO already). – pe-perry Mar 08 '18 at 05:50
0

I put the below code as an example. Here I look for all the keys that have 'Blue' in their values. Anyway, as said by COLDSPEED, this use of a dictionary is very unoptimized.

dic = {"a" : "Blue Jacket with buttons", "b" : "Green Jacket with a buttons",
  "c" : "Blue jacket"}
keywords = "Blue, Jacket, Buttons"
keywords = keywords.split(",")
for key, value in dic.iteritems():
    if keywords[0] in value:
        print key
el_Rinaldo
  • 970
  • 9
  • 26
0

Codes as the following:

def main():
    dic = {"a": "Blue Jacket with buttons", "b": "Green Jacket with a buttons",
           "c": "Blue jacket"}

    keywords = "Blue, Jacket, Buttons"
    keyword_items = [keyword.strip().lower() for keyword in keywords.split(",")]

    for key, value in dic.items():
        if all(keyword in value.lower() for keyword in keyword_items):
            print(key)
Menglong Li
  • 2,177
  • 14
  • 19
0

You could try using the difflib:

>>> import difflib
>>> dic = {"a" : "Blue Jacket with buttons", "b" : "Green Jacket with a buttons","c" : "Blue jacket"}
>>> keywords = "Blue, Jacket, Buttons"
>>> result = max((difflib.SequenceMatcher(a=keywords.lower(), b=value.lower()).ratio(),key) for key, value in dic.items())
>>> result
(0.8444444444444444, 'a')
>>> result[1]
'a'
Mike Peder
  • 728
  • 4
  • 8