Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python只查找字符串中单词的第一个实例_Python_Python 3.x_String_For Loop - Fatal编程技术网

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

Python只查找字符串中单词的第一个实例,python,python-3.x,string,for-loop,Python,Python 3.x,String,For Loop,这里是Python新手。 我想从列表中单词的第一个实例中提取句子。目前,它正在提取所有包含单词“dog”和“cat”的字符串。我尝试了I.split“”。[0]但这也不起作用。有人能帮忙吗 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

这里是Python新手。 我想从列表中单词的第一个实例中提取句子。目前,它正在提取所有包含单词“dog”和“cat”的字符串。我尝试了I.split“”。[0]但这也不起作用。有人能帮忙吗

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')

输出:

the dog was there

the cat is there too

the dog want want want was there

na

the dog was there
 the cat is there too
na
the dog was there
the cat is there too
na
期望输出:

the dog was there

the cat is there too

n/a (because choclate is not found)

谢谢大家!

无需对代码进行大量更改,您的输出可以通过在“单词”列表中使用“删除”来实现

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')

如果您反转循环,您可以使用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')
输出:

the dog was there

the cat is there too

the dog want want want was there

na

the dog was there
 the cat is there too
na
the dog was there
the cat is there too
na

一个可能的解决办法是跟踪你找到的单词。如果您不介意修改单词列表,可以这样做:

为了使代码更易于阅读,我还更改了一些变量名。 这段代码输出以下内容

the dog was there
the cat is there too
na

将代码缩减为一个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')
输出:

the dog was there

the cat is there too

the dog want want want was there

na

the dog was there
 the cat is there too
na
the dog was there
the cat is there too
na

反转循环也很好,因为同一个句子可能是第一个包含两个不同单词的句子-您希望能够再次返回它,这样就可以了。谢谢!它确实会改变列表中项目的顺序,是否可以保持顺序?您可以创建单词列表的副本并使用它。无论如何,这是明智的,因为删除会更改原始列表。如果所有的单词都被找到了一次,那么你的列表最后将是空的。