I have data naively collected from package dependency lists.
Depends: foo bar baz >= 5.2
I end up with
d = set([\'foo\',\'bar\',\'baz\',\'
No need for regular expressions here. Use str.isalpha. With and without list comprehensions:
my_list = ['foo','bar','baz','>=','5.2']
# With
only_words = [token for token in my_list if token.isalpha()]
# Without
only_words = filter(str.isalpha, my_list)
Personally I don't think you have to use a list comprehension for everything in Python, but I always get frowny-faced when I suggest map
or filter
answers.
How about
d = set([item for item in d if re.match("^[a-zA-Z]+$",item)])
that gives you just the values you want, back in d (the order may be different, but that's the price you pay for using sets.