I'm trying to weed out strings that contains "msi"
using regular expressions and a list comprehension. However, when I print the list, strings that contain "msi"
are still in the list. What exactly would the error be? This is my code:
spam_list = [l for l in spam_list if not re.match("msi", l)]
re.match()
matches from the beginning of the string. Use re.search()
, or even better, in
.
L = [l for l in L if "msi" not in l]
Since you're apparently looking through a list of filenames, you could also use endswith:
list = [l for l in list if l.endswith('.msi')]
Here is one way to filter a list by the file extension
import os
extensions = set(['.msi', '.jpg', '.exe'])
L = [l for l in L if os.path.splitext(l)[1] not in extensions]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.