Problem with filter()

Michael Spencer mahs at telcopartners.com
Tue Apr 3 17:32:08 EDT 2007


Boudreau, Emile wrote:
> Hey all,
>         So I'm trying to filter a list with the built-in function 
> filter(). My list looks something like this:
> ['logs', 'rqp-8.2.104.0.dep', 'rqp-8.2.93.0.dep', 
> 'rqp-win32-app-8.2.96.0-inst.tar.gz', 'rqp-win32-app-8.2.96.0-inst.tar.gz']
> 
> Calling filter like this: compFiles = filter(is_Dev, compFiles)
> compFiles is my list and is_dev is the following
> 
> def is_Dev(stringy):
>   print stringy
>   stringx = stringy.split('-')
>   if stringx[1] == r'win32':
>     if stringx[2] == r'app':
>       if stringx[4] == r'dev.tar.gz':
>         return 1
> 

Use fnmatch.filter instead?

  >>> source = ['logs', 'rqp-8.2.104.0.dep', 'rqp-8.2.93.0.dep', 
'rqp-win32-app-8.2.96.0-inst.tar.gz', 'rqp-win32-app-8.2.96.0-inst.tar.gz']
  >>>
  >>> import fnmatch
  >>> fnmatch.filter(source, '*-win32-app-*-inst.tar.gz')
  ['rqp-win32-app-8.2.96.0-inst.tar.gz', 'rqp-win32-app-8.2.96.0-inst.tar.gz']
  >>>

Michael




More information about the Python-list mailing list