python, cleaning a list
Trying to clean up a python list, I am able to remove exact string matches. How do I remove partial matches?
exclude = ['\n','Hits','Sites','blah','blah2','partial string','maybe here']
newlist = []
for item in array:
开发者_运维技巧 if item not in exclude:
newlist.append(item)
Problem here is "item not in exclude"... which does exact matching.
Should I use the following method:
s = "This be a string"
if s.find("is") == -1:
print "No 'is' here!"
else:
print "Found 'is' in the string."
In a way i answered my own question :) I guess is there an operand alternative to 'in' ?
Thanks
Try the following generator instead:
def remove_similar(array, exclude):
for item in array:
for fault in exclude:
if fault in item:
break
else:
yield item
Is this what you are searching for?
blacklist = ['a', 'b', 'c']
cleaned = []
for item in ['foo', 'bar', 'baz']:
clean = True
for exclude in blacklist:
if item.find(exclude) != -1:
clean = False
break
if clean:
cleaned.append(item)
print cleaned # --> ['foo']
I'm not sure what you're asking here. Do you want to filter out all elements in array
that are substrings of an element of exclude
? If so, you could replace your line
if item not in exclude:
with something like
if not any(item in e for e in exclude):
exclude = ['\n','Hits','Sites','blah','blah2','partial string','maybe here']
newlist = []
for item in array:
ok = True
for excItem in exclude:
if excItem in item:
ok = False
break
if ok: newlist.append(item)
how about:
all( s.find(e) == -1 for e in exclude )
which will return True
if none of the exclude strings are found as substrings in s
.
if by partial you mean that s
is a substring of e
, then:
not any( e.find(s) != -1 for e in exclude )
would return True
if s
is not found as a substring in any of the strings in exclude
精彩评论