2

I have a list containing strings that contain descriptions from a body of text that looks as follows:

stringlist = ['I have a dog and cat and the dog is seven years old', 'that dog is old']

and I need to filter these strings by a list of keywords that are located in another list:

keywords = ['dog', 'cat', 'old']

and appending each keyword to a row depending on how many times it is located in the string.

filteredlist = [['dog', 'dog', 'cat', 'old'], ['dog', 'old']]

I am splitting the strings in the stringslist and using list comprehension to check if the keyword is in the list but is not outputting correctly when I am looping through the keywords.

The code is working when I use one specific string to search for as follows:

filteritem = 'dog'
filteredlist = []
for string in stringlist:
    string = string.split()
    res = [x for x in string if filteritem in x]
    filteredlist.append(res)

The resulting filteredlist is as follows:

filteredlist = [['dog', 'dog'], ['dog']]

which appends the keyword for each instance that the keyword is located in the string sequence.

When I try looping through the keyword list as follows with a for loop the output loses the structure.

filteredlist = []
for string in stringlist:
    string = string.split()
    for keyword in keywords:
        res = [x for x in string if keyword in x]
        filteredlist.append(res)

Here is the output:

filteredlist =  [['dog', 'dog'], ['cat'], ['old'], [], ['dog'], [], ['old'], []]

I think I'm approaching this problem completely wrong so any other method or solution would be helpful.

2
  • I'm not quite clear on what your issue is. Is the filtered_list above what you want the output to look like? Commented Jun 30, 2015 at 18:49
  • the filteredlist at the top is the output i want it to look like. Commented Jun 30, 2015 at 18:59

1 Answer 1

3

You can write this as a nested list comprehension

>>> [[word for word in string.split() if word in keywords] for string in stringlist]
[['dog', 'cat', 'dog', 'old'], ['dog', 'old']]
Sign up to request clarification or add additional context in comments.

2 Comments

Beat me to it :) You may want to use a set.
thanks! knew there had to be a nested list comprehension in there somewhere.

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.