【问题标题】:Find from a list of strings, from a list of strings从字符串列表中查找,从字符串列表中查找
【发布时间】:2019-04-11 14:31:30
【问题描述】:

我需要帮助循环遍历句子/字符串列表,并根据另一个单词列表向前擦除字符串字符。

sentences = ['im not george smith my name is lucas mangulu thank you',
             'how shall i call you george smith oh okay got it'
             'we have detected a miyagi chung in the traffic flow']

words = ['lucas mangulu', 'george smith', 'miyagi chung']

我知道我必须为 sentences 列表中的每个元素循环。但是后来我被困在如何将 words 列表中的相同元素中的 find() 放入 sentences 列表中。这样最终的结果应该是:

sentences = ['im not george smith my name is',
             'how shall i call you'
             'we have detected a']

#OR

sentences = ['im not george smith my name is lucas mangulu',
             'how shall i call you george smith'
             'we have detected a miyagi chung']

【问题讨论】:

  • 回答如何找句子的问题,在python中'my name is lucas mangulu thank you'.find('lucas mangulu')会返回11,也就是'lucas mangulu'在字符串中的位置。从那里您可以使用子字符串操作来提取您需要的内容。
  • 您的示例输出混乱且令人困惑。在您的第一个输出中:['im not george smith my name is', 您离开了george smith,但在其他输出中您删除了所有名称。为什么?

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


【解决方案1】:

我很难准确理解您要查找的内容,但这里有一个简单的想法,即从 sentences 中的字符串中删除 words 中的字符串;这是对str.replace() 的多次调用。

>>> words = ['lucas mangulu', 'george smith', 'miyagi chung']
>>> original_sentences = [
...     'im not george smith my name is lucas mangulu thank you',
...     'how shall i call you george smith oh okay got it',
...     'we have detected a miyagi chung in the traffic flow',
... ]
>>> original_sentences
['im not george smith my name is lucas mangulu thank you',
 'how shall i call you george smith oh okay got it',
 'we have detected a miyagi chung in the traffic flow']

>>> sentences = list(original_sentences)                  # make a copy
>>> for i in range(len(sentences)):
...     for w in words:                                   # remove words
...         sentences[i] = sentences[i].replace(w, '')
...     while '  ' in sentences[i]:                       # remove double whitespaces
...         sentences[i] = sentences[i].replace('  ', ' ')
>>> sentences
['im not my name is thank you',
 'how shall i call you oh okay got it',
 'we have detected a in the traffic flow']

这是你打算做的吗?


如果你只想替换所有句子中的一个单词,你可以去掉嵌套的for循环:

>>> sentences = list(original_sentences)                  # make a copy
>>> word_to_remove = words[0]                             # pick one
>>> for i in range(len(sentences)):
...     sentences[i] = sentences[i].replace(word_to_remove, '')
>>> sentences
['im not george smith my name is  thank you',
 'how shall i call you george smith oh okay got it',
 'we have detected a miyagi chung in the traffic flow']

【讨论】:

  • 嗨,拉尔夫,看起来不错,唯一的事情是我必须替换每个元素。单词和句子的元素[0]。所以我对句子元素 [0] 的结果应该是:'我不是乔治史密斯,我的名字是'
  • @LucasMengual 我不认为我完全理解,但我编辑了我的答案以添加另一个想法。
【解决方案2】:

您为一个输入提供两个示例输出,这非常令人困惑。 以下代码可能会对您有所帮助,但我无法从逻辑上弄清楚如何与您的示例完全匹配。

话虽如此,我有一种预感,这就是你要找的东西。

import re
sentences = ['im not george smith my name is lucas mangulu thank you',
             'how shall i call you george smith oh okay got it',
             'we have detected a miyagi chung in the traffic flow',
             'Is this valid?']

words = ['lucas mangulu', 'george smith', 'miyagi chung', 'test']
ocurrences = []
for sentence in sentences:
    # If you want to find all occurences in a sentence this line will help you
    # ocurrences.append([(x.start(), x.end(), x.group()) for x in re.finditer('|'.join(words), sentence)])

    # Look for a word in this sentence (the first occurrence of that word)
    search_result = re.search('|'.join(words), sentence)
    # If we found a word in this sentence
    if search_result:
        ocurrences.append((search_result.start(), search_result.end(), search_result.group()))
    else: # No word found
        ocurrences.append((0, 0, None))

# Example output 1:
# oc in this case is (start_index, end_index, word_found) for each sentence.
for index, oc in enumerate(ocurrences):
  print(sentences[index][:oc[1]])

# Example output 2"
for index, oc in enumerate(ocurrences):
  print(sentences[index][:oc[0]])

示例输出 1:

我不是乔治·史密斯
我该怎么称呼你乔治·史密斯
我们检测到了宫城忠

示例输出 2:

我不是
我该怎么称呼你
我们检测到了一个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多