如何以不区分大小写的方式从列表中删除单词? [英] How do I remove words from a List in a case-insensitive manner?

查看:55
本文介绍了如何以不区分大小写的方式从列表中删除单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为words的列表,其中包含大写或小写的单词,或它们的某种组合.然后,我有另一个名为stopwords的列表,其中仅包含小写单词.现在,我想遍历stopwords中的每个单词,并以不区分大小写的方式从words中删除该单词的所有实例,但是我不知道该怎么做.有建议吗?

I have a list called words containing words which may be in upper or lower case, or some combination of them. Then I have another list called stopwords which contains only lowercase words. Now I want to go through each word in stopwords and remove all instances of that word from words in a case-insensitive manner, but I don't know how to do this. Suggestions?

示例:

words = ['This', 'is', 'a', 'test', 'string']
stopwords = ['this', 'test']

for stopword in stopwords:
    if stopword in words:
        words.remove(stopword);

print words

显示的结果是这样的:['This', 'is', 'a', 'string']

The result shown is this: ['This', 'is', 'a', 'string']

正确的回报应该是这样的:['is', 'a', 'string']

The correct return should have been this: ['is', 'a', 'string']

推荐答案

将您的单词小写,这样您就不必担心大小写了:

Make your word lowercase so you don't need to worry about casing:

words = ['This', 'is', 'a', 'test', 'string']
stopwords = {'this', 'test'}

print([i for i in words if i.lower() not in stopwords])

输出:

['is', 'a', 'string']

作为附加说明,每条@ cricket_007(并感谢@chepner进行了更正)注释,将停用词设置为一组将使其更具性能.注意上面对停用词的更改,使其成为一个集合而不是一个列表.

As an additional note, per @cricket_007 (and thanks to @chepner for the correction) comment, making stopwords a set would make it more performant. Notice the change to stopwords above making it a set instead of a list.

这篇关于如何以不区分大小写的方式从列表中删除单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆