【问题标题】:Extract single words or phrases from list of strings which are not in base string从不在基本字符串中的字符串列表中提取单个单词或短语
【发布时间】:2016-03-11 12:27:55
【问题描述】:

我想用 Python 构建一个脚本,它接受一个基本字符串并通过其他字符串列表运行它。该脚本应返回字符串中但不在基本字符串中的单词或短语的列表。

例子:

string = 'why kid is upset'

list_of_strings = ['why my kid is upset', 'why beautiful kid is upset',
                   'why my 15 years old kid is upset', 'why my kid is always upset']

应该返回

['my', 'beautiful', 'my 15 years old', 'always']

你会建议我研究什么库来解决这个问题?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

更新

此版本将所有已看到的单词添加到 exclude 集合中:

exclude = set('why kid is upset'.split())
list_of_strings = ['why my kid is upset', 
                   'why beautiful kid is upset', 
                   'why my 15 years old kid is upset',
                   'why my kid is always upset']
res = []
for item in list_of_strings:
    words = item.split()
    res.append(' '.join(word for word in words if word not in exclude))
    exclude.update(set(words))
print(res)

结果:

['my', 'beautiful', '15 years old', 'always']

这可行:

exclude = set('why kid is upset'.split())
list_of_strings = ['why my kid is upset', 
                   'why beautiful kid is upset', 
                   'why my 15 years old kid is upset',
                   'why my kid is always upset']
>>> [' '.join(word for word in item.split() if word not in exclude) for item
     in list_of_strings]
['my', 'beautiful', 'my 15 years old', 'my always']

【讨论】:

  • 谢谢迈克!有什么办法可以稍微改善一下吗?假设我们需要返回“15 岁”(而不是“我的 15 岁”)和“总是”(而不是“我的总是”),因为我们已经从之前的字符串中找到了“我的”。我是否只需要构建检查新创建的列表并仅返回唯一值的函数?
  • 添加了将已找到的单词添加到排除集的版本。
  • 我希望我拥有你 1/100 的知识。感谢您的支持!
  • 很好,它有帮助。顺便说一句,如果它解决了你的问题,你可以accept 一个答案。
【解决方案2】:

您不需要特殊的库。只需这样做:

def get_list(string, list_of_strings):
    split_list = string.split()
    return [" ".join(filter(lambda s: s not in split_list, string.split())) for string in list_of_strings)]

这可能有点难以阅读,因此您可以将其拆分:

def get_list(string, list_of_strings):
    split_list = string.split()
    new_list = []
    for string in list_of_strings:
        unseen_words = filter(lambda s: s not in split_list, string.split())
        unseen_sentence = " ".join(unseen_words)
        new_list.append(unseen_sentence)
    return new_list

【讨论】:

    【解决方案3】:

    当您在字符串列表中出现类似“为什么我 15 岁的孩子现在心烦意乱”之类的字符串时,我不确定您需要的格式

    无论如何,我没有要指出的库,这个小代码似乎可以解决你的问题:

    def stringNOTinbase(base,los):
        basewords = set(base.split(" ") )
        res = []
        for string in los:
            res.append( " ".join( [word for word in string.split(" ") if word not in basewords  ]   )   )
        return res
    

    如果你定义变量并像这样调用它:

    string = 'why kid is upset'
    
    list_of_strings = ['why my kid is upset', 'why beautiful kid is upset', 'why my 15 years old kid is upset', 'why my kid is always upset','why my 15 years old kid is upset now']
    
    print stringNOTinbase(string,list_of_strings)
    

    调用会输出这个:

    ['my', 'beautiful', 'my 15 years old', 'my always', 'my 15 years old now']
    

    解释:我取基本字符串并创建一个“集合”分割它; 然后将列表的每个字符串拆分为单词,并将不在集合中的单词添加到一个新列表中,然后再次用空格连接。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2015-05-08
      • 2021-03-15
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多