On Sat, 2008-04-12 at 13:35 -0700, John Poelstra wrote:
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?
removing items from a list you're working on means the index change in
place
so you'll end up skipping some items b/c the loop moves over them.
-sv