-1

I am thinking of an idea that I want to do for my ownself project during this quarantine phase . I am learning python by myself so I thought maybe I could see what I can do.

Question: I want to decipher large, but irregular count text and I want to find words in them, think of it like finding words. I may know the words that I find.

For example, I want to find

fruits = ["banana", "apples", "oranges"] 

in

Text = "sdasfdsfdscbananassafdfdsafscdfbfnhujuyjhtrgrfeaddaDWEAFSERGRapplesfsgrgvscfaefcwecfrvtbhytofsdasrangesdaeubfuenijnzcjbvnkMDLOwkdpoaDPOSKPKFEOFJsfjuf"

How could I do that?

Also its my 1st time posting here so I am not really confident about this community.

sorry & thank you

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    Welcome to SO! [tour] is a great place to start! – ggorlen May 10 '20 at 01:16
  • 2
    Check out the the 'for' statement and the 'in' operator. With those two, you could do this in 3 lines of code. Bonus points if you look up list comprehensions, you could pull it off in one! – djs May 10 '20 at 01:48
  • Does this answer your question? [Check if a Python list item contains a string inside another string](https://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string) – Rohan Bari May 10 '20 at 07:33

2 Answers2

0

Loop through all the words you want to find and check if they're in your text

for fruit in fruits:
    if fruit in Text:
        print(f"Found {fruit}")

Or, using a list comprehension:

found = [fruit for fruit in fruits if fruit in Text]
print(found)
BurningAshes
  • 16
  • 1
  • 1
0
fruits = ["banana", "apples", "oranges"]
text = "sdasfdsfdscbananassafdfdsafscdfbfnhujuyjhtrgrfeaddaDWEAFSERGRapplesfsgrgvscfaefcwecfrvtbhytofsdasrangesdaeubfuenijnzcjbvnkMDLOwkdpoaDPOSKPKFEOFJsfjuf"

for fruit in fruits:
    if fruit in text: 
        print(fruit, "True") 
    else:
        print(fruit, "False")
Tonechas
  • 13,398
  • 16
  • 46
  • 80