1

I'm trying to loop through a list of strings and match/print out any of those strings against a dictionary of words. I seem to get the following error and not too sure why.

Error: TypeError: 'in ' requires string as left operand, not list

Here is the current code i'm working with:

data = ["Great price on the dewalt saw", "cool deal, love it", "nice find", "definitely going to buy"]
words = {'price': ['price', 'compare', '$', 'percent', 'money']}

for d in data:
    for word in words.values():
        if word in d:
            print('Results:')
            print(d)

Ideally i'd like to print out all strings that contain any of the price key values.

3 Answers 3

2

word is returning a list. You need to loop/iterate over that list (word). You can accomplish it in the following manner -

data = ["Great price on the dewalt saw", "cool deal, love it", "nice find", "definitely going to buy"]
words = {'price': ['price', 'compare', '$', 'percent', 'money']}

for d in data:
    for word in words.values():
        for s in word :
            if s in d:
                print('Results:')
                print(d)

The above code will find if any string in array of values(that is - any of list from words.values()) in dictionary words is a part of any string in data or not.

Hope it helps !

Sign up to request clarification or add additional context in comments.

Comments

2

The issue you have here is that you have a list as a value, so your call to words.values() returns a list that contains another list internally. You could change that to for word in words['price'] if you will only ever have a price key, or you could change it like so:

>>> words = {'price': ['price', 'compare', '$', 'percent', 'money']}
>>> [word for wordlist in words.values() for word in wordlist]
['price', 'compare', '$', 'percent', 'money']

Comments

0

May be its good idea to improve efficiency of accepted answer.

Below is the just an psuedocode to improve time complexity(based on inputs).

visited_dict = {}
results = {}
for d in data:
    for k, word in words.items():
        can_stop = False
        res_val = []
        for s in word:
            # if same words from another key is searched
            # Ex: words = {'price': ['price', 'compare', '$', 'percent', 'money'],
            #               'price2':[ 'price', 'something', 'somethingelse']}
            # In this case - get the result from visited dictionary to avoid 
            if s in visited_dict and s not in visited_dict[s]:
                # save it to results 
                # skip further steps
                continue

            if s in d and s not in res_val:
                # store it results
                res_val.append(d)
                # update visited dict
                visited_dict[s] = d
            # Save the result to final key to result map
            results[k] = res_val  # {'price': 'Great price on the dewalt saw'}

            # To avoid duplicate
            if res_val:
                break

Note: its not tested or completely implemented code. Yes, once implemented properly it may increase space complexity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.