help with simple script and strings

John Poelstra poelstra at redhat.com
Sat Apr 12 20:35:33 UTC 2008


I'm just getting started with python.  Curious what I need to change to 
properly filter out only strings starting with "200"

script:
#!/usr/bin/python

directories = ['20080412', '20080324', 'blahblah', 'latest-dir', 
'rawhide-20080410', 'rawhide-20080411', 'rawhide-20080412' , '20080401']

print 'directories == %s' % directories

for directory in directories:
     print 'processing directory %s ' % directory
     if not directory.startswith('200'):
         directories.remove(directory)
         print 'removed == %s' % directory

print 'directories == %s' % directories

~~~~~~~~~~~~~~~~~~~~~~~~~
output:
$ python dir-filter.py
directories == ['20080412', '20080324', 'blahblah', 'latest-dir', 
'rawhide-20080410', 'rawhide-20080411', 'rawhide-20080412', '20080401']
processing directory 20080412
type == <type 'str'>
processing directory 20080324
type == <type 'str'>
processing directory blahblah
type == <type 'str'>
removed == blahblah
processing directory rawhide-20080410
type == <type 'str'>
removed == rawhide-20080410
processing directory rawhide-20080412
type == <type 'str'>
removed == rawhide-20080412
directories == ['20080412', '20080324', 'latest-dir', 
'rawhide-20080411', '20080401']

~~~~~~~~~~~~~~~~~~~~

why do 'rawhide-20080411' and 'latest-dir' remain?

Thanks,
John




More information about the python-devel mailing list