Home > Software engineering >  How to print data if only matching string found in list?
How to print data if only matching string found in list?

Time:05-07

key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

I tried this but nothing is print:

if title1 in key_word:
   print(title1)

CodePudding user response:

You have to check if your key word is in the title, and not the other way around.

key_words = ["apple", "boy"]
titles = [
    "Where the boy", 
    "The Boy playing cricket", 
    "hello world", 
    "I want to buy apple vinegar"
]

for title in titles:
    for key_word in key_words:
        if key_word.lower() in title.lower():
            print(title)
            break

For each title, for each keyword, check if the lowercase version of the keyword is in the lowercase version of the title - if it is, print the title and stop going through the keywords (which is what break does).

This will start with I want to buy apple vinegar, then check if apple is in the title - which it is, and then print that. For the next entry, The Boy playing cricket, we check if apple is in the list, which it isn't - so we move on to the next word, boy - which is in the list.

I removed your duplicate entries since lower makes those redundant.

I also changed your titles into a list - instead of having title1, title2, etc. This makes it possible to go through the list without having to specify each variable name, and lets you extend the set of titles being checked later easily.

Another small detail is that this only checks if the letters appear in the same order; it does not check that they constitute a word (so a title containing "apples" would get a match, so would a title containing "tomboy"). You can work around this by splitting your title into words by using .split() first, and then checking if your keyword is in that list.

Another option is using regular expressions, but that is a more advanced technique - you can consider doing that later when you have more experience.

CodePudding user response:

key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

for word in key_word:
    if word in title1:
        print(title1)

If you want to check all titles, put them in a list:

key_word = ["apple","Apple","Boy","boy"]

titles = [
    "Where the boy",
    "The Boy playing cricket",
    "hello world",
    "I want to buy apple vinegar",
]

for word in key_word:
    for title in titles:
        if word in title:
            print(title)

CodePudding user response:

It looks as though you want to print any title that contains any of the key words.

Start by iterating over the titles.

Then:

key_word = ["apple","Apple","Boy","boy"]

title1 = "Where the boy"
title2 = "The Boy playing cricket"
title3 =  "hello world"
title4 = "I want to buy apple vinegar"

for title in title1, title2, title3, title4:
    if any(kw in title for kw in key_word):
        print(title)

Output:

Where the boy
The Boy playing cricket
I want to buy apple vinegar
  • Related