简体   繁体   English

Python 仅查找字符串中单词的第一个实例

[英]Python find ONLY the first instance of a word in a string

Newbie to Python here. Python 新手在这里。 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'.目前,它正在提取所有包含单词“dog”和“cat”的字符串。 I tried (i.split('.')[0]) but this is not working either.我试过(i.split('.')[0])但这也不起作用。 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:如果您反转循环,则可以使用break转到下一个单词:

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:如果您可以修改words列表,则可以这样做:

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:缩小你的代码(只有一个 for 循环),你可以在单词列表上使用pop()从那里删除一个项目:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM