list comparison help?

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Apr 14 06:45:01 EDT 2007


7stud:

> prefixes = [ "the", "this", "that", "da", "d", "is", "are", "r",
> "you", "u"]
> sentence = "what the blazes is the da this da this the"
> sentence = sentence.split()
> result = [word for word in sentence if word not in prefixes]
> print result

If prefixes becomes long enough (let's say > 20) then it's faster to
use a set:

nowords = [ "the", "this", "that", "da", "d", "is", "are", "r", "you",
"u"]
prefixes = set(nowords)

Bye,
bearophile




More information about the Python-list mailing list