Problem with filter()
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 For some reason I only get 'logs' printed when is_Dev is called. (ie. It's only going through is_dev once) Does anyone see something wrong that I'm not seeing?? Thanks, Emile Boudreau This message may contain privileged and/or confidential information. If you have received this e-mail in error or are not the intended recipient, you may not use, copy, disseminate or distribute it; do not open any attachments, delete it immediately from your system and notify the sender promptly by e-mail that you have done so. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
RE: Problem with filter()
Sorry folks my mistake def is_dev should be: def is_Dev(stringy): stringx = stringy.split('-') if stringx[0] == '': if stringx[1] == r'win32': if stringx[2] == r'app': if stringx[4] == r'dev.tar.gz': return 1 But now the results of the filter is an empty list and i know it shouldn't be. Emile Boudreau From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Boudreau, Emile Sent: Tuesday, April 03, 2007 10:52 AM To: python-list@python.org Subject: Problem with filter() 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 For some reason I only get 'logs' printed when is_Dev is called. (ie. It's only going through is_dev once) Does anyone see something wrong that I'm not seeing?? Thanks, Emile Boudreau This message may contain privileged and/or confidential information. If you have received this e-mail in error or are not the intended recipient, you may not use, copy, disseminate or distribute it; do not open any attachments, delete it immediately from your system and notify the sender promptly by e-mail that you have done so. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with filter()
On 4/3/07, Boudreau, Emile [EMAIL PROTECTED] wrote: Sorry folks my mistake def is_dev should be: def is_Dev(stringy): stringx = stringy.split('-') if stringx[0] == '': if stringx[1] == r'win32': if stringx[2] == r'app': if stringx[4] == r'dev.tar.gz': return 1 But now the results of the filter is an empty list and i know it shouldn't be. Well, you haven't told us what you expect the result to be, but as far as I can tell, an empty list is the correct answer, because you don't have any files in your list that match your rules. This line if stringx[0] == '': is very likely your problem. That requires that your file start with a '-', so that after you split it stringx[0] will equal ''. You may need something more like this: def is_Dev(stringy): print stringy try: stringx = stringy.split('-') if stringx[1] == r'win32': if stringx[2] == r'app': if stringx[4] == r'dev.tar.gz': return 1 except IndexError, e: pass That will still give you an empty list when run on your proposed inputs though, because none of them are of the form something-win32-app-something-dev.tar.gz If this doesn't help, please give us some sample inputs and what you expect the output to be. -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
Problem with filter()
Sorry folks my mistake def is_dev should be: def is_Dev(stringy): stringx = stringy.split('-') if stringx[0] == '': if stringx[1] == r'win32': if stringx[2] == r'app': if stringx[4] == r'dev.tar.gz': return 1 But now the results of the filter is an empty list and i know it shouldn't be. I think the problem is the first if - using the list from your previous mail, stringx[0] is never empty. So the filter fn never returns true, the return list is empty... stringx[4] never matches either. This is what I did: compFiles=['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 '] for i in compFiles: print i.split('-') # ie the equivalent of stringx ['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'] Birone PS - I'm new at this. Please forgive me if I got it totally wrong! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with filter()
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 -- http://mail.python.org/mailman/listinfo/python-list