繁体   English   中英

从Python列表中删除单词

[英]Removing words from list in python

我有一个列表“ abc”(字符串),并且我试图从列表“ abc”中删除列表“ stop”中存在的某些单词以及abc中存在的所有数字。

abc=[ 'issues in performance 421',
 'how are you doing',
 'hey my name is abc, 143 what is your name',
 'attention pleased',
 'compliance installed 234']
stop=['attention', 'installed']

我正在使用列表推导将其删除,但是下面的代码无法删除该单词。

new_word=[word for word in abc if word not in stop ]

结果:(注意词仍然存在。)

['issues in performance',
 'how are you doing',
 'hey my name is abc, what is your name',
 'attention pleased',
 'compliance installed']

所需的输出:

 ['issues in performance',
     'how are you doing',
     'hey my name is abc, what is your name',
     'pleased',
     'compliance']

谢谢

您需要过滤掉stop单词,然后将每个短语拆分为单词,然后将单词重新组合为短语。

[' '.join(w for w in p.split() if w not in stop) for p in abc]

输出:

['issues in performance', 'how are you doing', 'hey my name is abc, what is your name', 'pleased', 'compliance installed']

只需要使用set就可以解决这个问题。 因为您可能在每个项目中都包含多个单词,所以您不能in使用。 您应该将set&结合使用以获取公开字词。 如果存在公共词,并且您设置的stop词将返回True 因为您只关心其余部分,所以if not这里,我们可以使用。

new_word=[word for word in abc if  not set(word.split(' ')) & set(stop)]

更新

如果您还想删除所有包含数字项,则只需执行以下操作即可:

new_word=[word for word in abc if  not (set(word.split(' ')) & set(stop) or any([i.strip().isdigit() for i in word.split(' ')]))]

这是一个解决方案,将简单的正则表达式与re.sub方法配合使用。 此解决方案也会删除数字。

import re

abc=[ 'issues in performance 421',
 'how are you doing',
 'hey my name is abc, 143 what is your name',
 'attention pleased',
 'compliance installed 234']
stop=['attention\s+', 'installed\s+', '[0-9]']

[(lambda x: re.sub(r'|'.join(stop), '', x))(x) for x in abc]


'Output':
['issues in performance ',
'how are you doing',
 'hey my name is abc,  what is your name',
 'pleased',
 'compliance ']
list1 = []
for word in abc:
    word1 = ''
    for remove_word in stop:
        word1 = remove_word
        word1 = word.replace(word1, '')
    list1.append(word1)

这至少是我要做的:

abc=[ 'issues in performance 421',
    'how are you doing',
    'hey my name is abc, 143 what is your name',
    'attention pleased',
    'compliance installed 234'
]
stop=['attention', 'installed']
for x, elem in enumerate(abc):
    abc[x] = " ".join(filter(lambda x: x not in stop and not x.isdigit(), elem.split()))
print(abc)

结果:

['issues in performance',
    'how are you doing',
    'hey my name is abc, what is your name',
    'pleased',
    'compliance']

希望它能以任何方式帮助您:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM