0

I'm writing a program that makes a dictionary where there are keys with values that have long text sentences.

The goal of the program is for me to text a number, Python scrapes the website and compiles a dictionary from the scrape, and then searches the values for the string from my text. As an example, let's say the dictionary looks like as follows:

myDict = {"Key1": "The dog ran over the bridge", 
          "Key2": "The cat sleeps under the rock", 
          "Key3": "The house is dark at night and the dog waits"}

Let's say I want to search the values and return the key to me that has the relevant string. So if I text 'dog' to the number, it scans the dictionary for all values that have 'dog' and then returns the key with the relevant value, in this case 'Key1' and 'Key3'.

I've tried a few methods of doing this from elsewhere on stack exchange, such as here: How to search if dictionary value contains certain string with Python

However, none of those worked. It either gives me only the first Key regardless of the string, or it returned an error message.

I'd like it to be case insensitive, so I imagine I need to use re.match, but I'm having trouble utilizing regex with this dict and getting any useful returns.

Georgy
  • 12,464
  • 7
  • 65
  • 73

4 Answers4

1

The solution that you looked at was searching for each letter. My solution fixed that by looking at the whole string and it returns an array instead of the first value.

myDict = {"Key1": "The dog ran over the bridge",
    "Key2": "The cat sleeps under the rock",
    "Key3": "The house is dark at night and the dog waits"}

def search(values, searchFor):
    listOfKeys = []
    for k in values.items():
        if searchFor in k[1]:
            listOfKeys.append(k[0])
    return listOfKeys

print(search(myDict, "dog"))

and it will output:

['Key1', 'Key3']
WyattBlue
  • 591
  • 1
  • 5
  • 21
  • 1
    Minor improvement: use `for key, value in values.items()` and then use `key` and `value` rather than `k[0]` and `k[1]` in the loop. Using meaningful names (rather than 0 and 1) can make the code easier to follow. – Jiří Baum Jun 04 '20 at 23:31
1

Here's a version using list comprehension. https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

d = {
    "Key1": "The dog ran over the bridge",
    "Key2": "The cat sleeps under the rock",
    "Key3": "The house is dark at night and the dog waits",
}


def find(values, key):
    key = key.lower()
    return [k for k, v in values.items() if key in v.lower()]


print(find(d, "dog"))

If this is going to be something that's done often it will be worth making sure the dic values are all lower case to start with and store them this way.

d = {
    "Key1": "The dog ran over the bridge",
    "Key2": "The cat sleeps under the rock",
    "Key3": "The house is dark at night and the dog waits",
}

for k in d:
    d[k] = d[k].lower()


def find(values, key):
    key = key.lower()
    return [k for k, v in values.items() if key in v]


print(find(d, "dog"))
Kassym Dorsel
  • 4,773
  • 1
  • 25
  • 52
0

I find it a bit easier to use Array.filter():

const myDict = {"Key1": "The dog ran over the bridge", 
          "Key2": "The cat sleeps under the rock", 
          "Key3": "The house is dark at night and the dog waits"}

const searchString = "dog";
const res = Object.keys(myDict).filter((key) => myDict[key].includes(searchString));

console.log(res);
Frode
  • 1
0

Iterating Through .items() should give you the result:

myDict = {"Key1": "The dog ran over the bridge", 
          "Key2": "The cat sleeps under the rock", 
          "Key3": "The house is dark at night and the dog waits"}

for key, value in myDict.items():
    if "dog" in value.lower():
        print(key) 
Hermann12
  • 1,709
  • 2
  • 5
  • 14