python,清理列表

2024-01-18

尝试清理 python 列表,我能够删除精确的字符串匹配。如何删除部分匹配项?

exclude = ['\n','Hits','Sites','blah','blah2','partial string','maybe here']
newlist = []
for item in array:
    if item not in exclude:
        newlist.append(item)

这里的问题是“项目不在排除范围内”......它进行精确匹配。

我应该使用以下方法:

s = "This be a string"
if s.find("is") == -1:
    print "No 'is' here!"
else:
    print "Found 'is' in the string."

在某种程度上,我回答了我自己的问题:)我想有一个操作数可以替代 'in' 吗?

Thanks


尝试使用以下生成器:

def remove_similar(array, exclude):
    for item in array:
        for fault in exclude:
            if fault in item:
                break
        else:
            yield item
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python,清理列表 的相关文章

随机推荐