【问题标题】:Python extract n sentences from string after the first instance of the string has been foundPython在找到字符串的第一个实例后从字符串中提取n个句子
【发布时间】:2020-11-06 19:09:52
【问题描述】:

我想提取在列表“单词”中找到元素的第一个实例。我下面的代码正确地做到了这一点。 但是对于列表“单词”中的某些项目(例如狗和猫),我想提取字符串的第一次出现,以及之后的 3 个句子 - 请有人能告诉我如何做到这一点吗?

我的代码如下:

text= 'the dog was there. the cat is there too. python is the best. she has a kitten. cat is cute. the dog want want want was there. my dog is a puppy. chocolate brownies. flower power.'

words=['dog', 'chocolate', 'cat' ,'flower']
lst=[]

for j in words:
    for i in text.split('.'):
        if j in i:
            lst.append (i.split('.')[0]) 
            break
else:
    None
     
print(lst)

期望的输出:

['the dog was there. the cat is there too. python is the best' , 'chocolate brownies', 'the cat is there too. python is the best. she has a kitten', 'flower power']

我尝试过的(索引) - 不确定我做错了什么

if j[0] in i:
 lst.append( i.split('.')[0:3]
if j[2] in i:
 lst.append. i.split('.')[0:3]

谢谢

【问题讨论】:

  • 你想要找到单词的句子 i,还是想要第一个句子 (0),如果你找到单词?
  • 我要找到单词的第一个句子,然后对于一些元素,找到第一个句子+之后提取3个句子
  • @qwerty12 你如何确定哪些元素应该在之后提取 3 个句子?除非您知道这一点,否则不可能在单个 for 循环中完成您的要求。
  • 是的,在这个例子中是它的狗和猫

标签: python python-3.x string list for-loop


【解决方案1】:

为了提取第一个实例,您的代码确实有效,但有一些不必要的代码。

只需附加i,而不是i.split('.')[0]。您的i 已被拆分。

for j in words:
    for i in text.split('.'):
        if j in i:
            lst.append(i) 
            break

现在提取n个句子,

for j in words:
    for index, i in enumerate(text.split('.')):
        if j in i:
            if j == "dog" or j == "cat":
               lst.append('.'.join(text.split('.')[index:index+n]))
            else:
               lst.append(i) 
            break

这里使用枚举,我们跟踪句子的索引。如果我们遇到一个带有狗或猫的句子,而不是追加我们添加长度为 n 的子列表。

【讨论】:

  • 谢谢!正是我所追求的。是否可以将 dog 输出作为列表中的一个长元素附加,cat 输出也一样 - 目前它在列表中超过 3 个不同的元素?原因是我要附加到具有设置列名的数据框 - 再次感谢!
  • 一个长元素是什么意思?
  • 所以把它放到一个长字符串中,而不是 diff 元素中:示例输出:output=['the dog was there. dog. the cat is there too', ' chocolate brownies', ' the cat is there too.python is the best.cat has a kitten']
【解决方案2】:

任务1:拆分段落,得到句子列表。对于列表词中的每个查询词,检查它是否存在于句子列表中,如果存在则将其添加到结果中。

text= 'the dog was there. the cat is there too. python is the best. she has a kitten. cat is cute. the dog want want want was there. my dog is a puppy. chocolate brownies. flower power.'

words=['dog', 'chocolate', 'cat' ,'flower']

lst=[]

list_sentences = text.split('.')

for query_word in words:
    for sentence in list_sentences:
        if query_word in sentence:
            lst.append (sentence) 
            break
else:
    None
     
print(lst)

重要提示:你的代码可以工作,但它是错误的,因为你有一个句子 i,所以你直接附加它,在你的代码中你托盘拆分它 lst.append(i.split('.')[0]) 并且因为句子i 不包含 '。',你最后得到的结果是一样的。

任务 2:当您附加所有三个句子时,这是您的第二个任务。完全相同的代码,只需添加从找到查询词的句子开始的所有三个句子。我们使用 enumerate 来跟踪索引。

text= 'the dog was there. the cat is there too. python is the best. she has a kitten. cat is cute. the dog want want want was there. my dog is a puppy. chocolate brownies. flower power.'

words=['dog', 'chocolate', 'cat' ,'flower']

list_sentences = text.split('.')

list_task2 = []
query_word = words[0] # just example, you can change it

for i, sentence in enumerate(list_sentences):
        if query_word in sentence:
            #list_task2.append (list_sentences[i:i+3])
            list_task2 = list_task2 + list_sentences[i:i+3]
            break
print(list_task2)        

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 2013-03-31
    • 1970-01-01
    • 2022-12-18
    • 2020-02-14
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多