PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] How to return keywords in a list from multiple sentences?

 July 09, 2022     for-loop, keyword, list, python     No comments   

Issue

I have a list of keywords which I wish to match in a list of sentences. If found within that sentence than return the found keyword in a list.

What I have tried:

sentence = df['List of Content']
list_of_words = ['keyword1','keyword2', 'keyword3']

This below works if I choose only one row:

[word for word in list_of_words if word in sentence[0]

and outputs

output: ['keyword1', 'keyword3']

The desirable output for all the rows, is a list of keywords that match in the sentence. Something like this:

matching_keywords = [['keyword1', 'keyword3'],['keyword2, 'keyword3'],['keyword1', 'keyword2']..]

However, when I run the for loop in the entire list it just outputs an empty list []


I have also tried a nested for loop:

kwords = []
for row in MCC:
    for x in list_of_words:
        if x in row:
            kwords.append(x)

It either gives me an empty bracket list again [] or it just creates a long list of the keywords repeating themselves.

What is the mistake am I making? Anyone can try to help me with the logic/solution.


Solution

You could extend your initial approach by doing the following.

[[word for word in list_of_words if word in row] for row in sentence]

Explanation: This amounts to nested list comprehension. For each row, we want a list of keywords that appear in that row. With list comprehension, this should be written as

[<list of keywords in row> for row in sentence]

On the other hand, if you have a specific row that you're looking at (for instance, row = sentence[0]), then as you state in your question the list of keywords that appear in this row can be obtained with [word for word in list_of_words if word in row]. Putting this together leads to the result I wrote above, namely

[[word for word in list_of_words if word in row] for row in sentence]


Answered By - Ben Grossmann
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing