-1

I have a problem with my list comprehension in python. I have a string variable with search queries, like this:

queries = 'news, online movies, weather, golden rush, online sports, price 
of the golden ring, today weather, python'

And I have a list of 2 elements:

words = [ 'online', 'golden' ]

And I need to filter queries string with the list words, such that the final result will not include queries which have 'online' and 'golden' in its content.

I've tried this, but it doesn't work properly:

filteredquerry = [] 
queriesNew = queries.split(',')

for x in queriesNew:
    if x not in words:
        filteredquerry.append(x)
    else:
        break

print(filteredquerry)

Also I have tried another way of list 'filtering' using a list methods, but it gives me an error or gives back an empty list:

print( [ x for x in queries if x not in words ]

The expected result should look like this:

filteredquerry = ['news', 'weather', 'today weather', 'python']
Leonard
  • 33
  • 1
  • 7
  • why is pandas tagged? is this a pandas series?? – anky Jun 09 '19 at 15:02
  • sorry I used it wrong – Leonard Jun 09 '19 at 15:04
  • `print( [ x for x in queries if x not in words ]` was close. apart from missing the ending bracket, you overlooked the fact that queries is just a string, and required the splitting on comma that you've done in the other approach. – Paritosh Singh Jun 09 '19 at 15:05
  • Why do you have a `break` in the first attempted solution? This will stop the loop altogether, without processing the remaining elements. Also, in your second attempted solution, `queries` is a string, not a list, so looping over it will give you the individual characters. – John Gordon Jun 09 '19 at 15:07
  • You need to split your string into a list of words like this `queries.split(", ")`. Then `print([ x for x in queries if x not in words ])` will work – Anjum Sayed Jun 09 '19 at 15:08
  • @AnjumSayed I tried this but it doesn't work. It gives me back a list with individual letters, but not a filtered querry. – Leonard Jun 09 '19 at 15:12
  • See the dupes. Split your string at `,` use list comprehension or filter to create a new list without the things you need. The dupe filters existing items from a list if they contain certain string - you need to negate it to filter existing items from a list if they are _not_ contained in the second list. – Patrick Artner Jun 09 '19 at 15:14
  • 2
    It boils down to: `filtered_query = [item for item in queries.split(",") if not any(w in item for w in words) ]` – Patrick Artner Jun 09 '19 at 15:17
  • A bit wordier than the `any` technique, but if words has only two elements, then `[x for x in queries.split(', ') if words[0] not in x and words[1] not in x]` will also work. – lmo Jun 09 '19 at 15:30

1 Answers1

1

try this.

    queries = 'news, online movies, weather, golden rush, online sports, price of the golden ring, today weather, python'
    queries = queries.split(',')
    words = [ 'online', 'golden' ]
    print([x for x in queries if not any(word in x for word in words)])
    # ['news', ' weather', ' today weather', ' python']

python any() docs see https://docs.python.org/3/library/functions.html#any

tink
  • 123
  • 6