代码之家  ›  专栏  ›  技术社区  ›  Sociopath

如果字符串中包含停止字,则从该字符串中移除元素[重复]

  •  1
  • Sociopath  · 技术社区  · 6 年前

    我有如下列表:

    lst = ['for Sam', 'Just in', 'Mark Rich']
    

    我正在尝试从包含 stopwords .

    因为列表中的第一个和第二个元素包含 for in 哪些是 停用词 ,它会回来的

    new_lst = ['Mark Rich'] 
    

    我试过什么

    from nltk.corpus import stopwords
    
    stop_words = set(stopwords.words('english'))
    
    lst = ['for Sam', 'Just in', 'Mark Rich']
    new_lst = [i.split(" ") for i in lst]
    new_lst = [" ".join(i) for i in new_lst for j in i if j not in stop_words]
    

    其输出为:

    ['for Sam', 'Just in', 'Mark Rich', 'Mark Rich']
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   jpp    6 年前

    你需要一个 if 语句而不是额外嵌套:

    new_lst = [' '.join(i) for i in new_lst if not any(j in i for j in stop_words)]
    

    如果你想利用 set ,你可以使用 set.isdisjoint :

    new_lst = [' '.join(i) for i in new_lst if stop_words.isdisjoint(i)]
    

    下面是一个演示:

    stop_words = {'for', 'in'}
    
    lst = ['for Sam', 'Just in', 'Mark Rich']
    new_lst = [i.split() for i in lst]
    new_lst = [' '.join(i) for i in new_lst if stop_words.isdisjoint(i)]
    
    print(new_lst)
    
    # ['Mark Rich']
    
        2
  •  1
  •   yatu Sayali Sonawane    6 年前

    你可以用列表理解和使用 sets 要检查两个列表中是否有任何单词相交:

    [i for i in lst if not set(stop_words) & set(i.split(' '))]
    ['Mark Rich']]