[Tutor] Hmmm...
Tesla Coil
tescoil@irtc.net
Fri, 09 Mar 2001 10:55:35 -0500
On 8 Mar 2001, Britt Green wrote:
> Actually, I figured out what I needed to
> do on my own!
>
> import string
>
> theFruits = ['apple', 'banana', 'pineapple', 'pear', 'orange']
>
> chosen = []
>
> for stuff in theFruits:
> if stuff.endswith('e'):
> chosen.append(stuff)
>
> print chosen
>
> When run, only the elements of theFruits that end with
> an 'e' will get added to the list of chosen. Is there
> a better way to do this?
No need to import string for the above.
An old-fashioned (<2.0) method that still works:
for stuff in theFruits:
if stuff[-1] == 'e':
chosen.append(stuff)
You might be interested in this:
>>> import re
>>> basket = ['apple', 'banana', 'pineapple', 'pear', 'orange']
>>> chosen = []
>>> pick = re.compile('an')
>>> for fruits in basket:
... if pick.search(fruits):
... chosen.append(fruits)
>>> chosen
['banana', 'orange']
See:
http://www.python.org/doc/howto/regex/regex.html