Newbie to Python here. I would like to extract the sentence where the first instance of the words in the list have been found. Currently, it is extracting all strings which have the word 'dog' and 'cat'. I tried (i.split('.')[0])
but this is not working either. Can anyone help please?
text= 'the dog was there. the cat is there too. python is the best. the dog want want want was there. '
lst=[]
words=['dog', 'cat', 'chocolate']
for i in text.split('.'):
for j in words:
if j in i:
print(i.split('.')[0])
lst.append (i.split('.')[0])
else:
lst.append('na')
print('na')
Output:
the dog was there
the cat is there too
the dog want want want was there
na
Desired output:
the dog was there
the cat is there too
n/a (because choclate is not found)
thank you!
Without making a lot of changes to your code, your output can be achieved by using 'remove' on your 'words' list.
text= 'the dog was there. the cat is there too. python is the best. the dog want want want was there. '
lst=[]
words=['dog', 'cat', 'chocolate']
for i in text.split('.'):
for j in words:
if j in i:
print(i.split('.')[0])
words.remove(j) # this will remove the matched element from your search list
lst.append (i.split('.')[0])
else:
lst.append('na')
print('na')
If you reverse your loops, you can just use break
to go to the next word:
text= 'the dog was there. the cat is there too. python is the best. the dog want want want was there. '
lst=[]
words=['dog', 'cat', 'chocolate']
for j in words: # each word
for i in text.split('.'): # each sentence
if j in i:
print(i.split('.')[0])
lst.append (i.split('.')[0])
break # next word
else:
lst.append('na')
print('na')
Output:
the dog was there
the cat is there too
na
A possible solution could be keeping track of which words you have found. This could be done like this, if you are fine with modifying the words
list:
text= 'the dog was there. the cat is there too. python is the best. the dog want want want was there. '
lst=[]
words=['dog', 'cat', 'chocolate']
for sentence in text.split('.'):
sentence = sentence.strip() # Remove whitespace around sentence
for word in words:
if word in sentence:
print(sentence)
lst.append(sentence)
# Remove the found word from words
words.remove(word)
else:
lst.append('na')
print('na')
I also changed some variable names in order to make the code more easily readable. This piece of code outputs the following
the dog was there
the cat is there too
na
Shrinking your code down (just one for loop), you can use pop()
on the word list to remove an item from there:
text = 'the dog was there. the cat is there too. python is the best. the dog want want want was there. '
sentences = text.split('.')
words=['dog', 'cat', 'chocolate']
for sentence in sentences:
# Takes the first word as long as there are items in the list!
word = words.pop(0) if words else None
if word and word in sentence:
print(sentence.strip()) # Removes whitespaces arround the sentence
else:
print('na')
Output:
the dog was there
the cat is there too
na
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.