GHZ | 28 Aug 12:16

filter in for loop

I would like to say something like:

for x in l if <expression>:
    <do something>

e.g.

for filename in os.listdir(DIR) if filename[-4:] == '.xml':
    <do something>

instead of having to say:

for filename in os.listdir(DIR):
    if filename[-4:] == '.xml':
        <do something>

or

for filename in (f for f in os.listdir(DIR) if f[-4] == '.xml'):
    <do something>

is there a shortcut I'm missing?
--
http://mail.python.org/mailman/listinfo/python-list

Jerry Hill | 28 Aug 17:12

Re: filter in for loop

On Thu, Aug 28, 2008 at 6:20 AM, GHZ <geraint.williams <at> gmail.com> wrote:
> is there a shortcut I'm missing?

Well, there's another way to do it, using the filter built in function:

for filename in filter(lambda f:f.endswith('.xml'), os.listdir('.')):
	print filename

You can do the same thing with itertools.ifilter if you need to deal
with arbitrary iterables.  I think your first example of:

for filename in os.listdir('.'):
   if filename.endswith('.xml'):
       <do something>

Is the most readable though.

--

-- 
Jerry
--
http://mail.python.org/mailman/listinfo/python-list

Emile van Sebille | 28 Aug 21:54

Re: filter in for loop

Jerry Hill wrote:
> On Thu, Aug 28, 2008 at 6:20 AM, GHZ <geraint.williams <at> gmail.com> wrote:
>> is there a shortcut I'm missing?
> 

import glob

Emile

--
http://mail.python.org/mailman/listinfo/python-list

Matthew Woodcraft | 28 Aug 21:48

Re: filter in for loop

GHZ <geraint.williams <at> gmail.com> writes:

> I would like to say something like:
>
> for filename in os.listdir(DIR) if filename[-4:] == '.xml':
>     <do something>
>
>
> instead of having to say:
>
> for filename in os.listdir(DIR):
>     if filename[-4:] == '.xml':
>         <do something>

If the reason you don't like this one is the extra indentation, one
possibility is

for filename in os.listdir(DIR):
    if filename[-4:] != '.xml':
        continue
    <do something>

-M-
--
http://mail.python.org/mailman/listinfo/python-list

Dennis Lee Bieber | 29 Aug 00:11

Re: filter in for loop

On Thu, 28 Aug 2008 19:48:30 GMT, Matthew Woodcraft
<matthew <at> woodcraft.me.uk> declaimed the following in comp.lang.python:

	Following seems to work (did 2.4 already have generator expressions?
I tend to stick to list-comps usually, but felt this example might not
want to create the list)

>>> import os
>>> for fid in (x for x in os.listdir("") if x.endswith(".py")):
... 	print fid
... 
booklist.py
filter.py
old_filter.py
Script1.py
Script11.py
tgsetup.py
>>> 
--

-- 
	Wulfraed	Dennis Lee Bieber		KD6MOG
	wlfraed <at> ix.netcom.com		wulfraed <at> bestiaria.com
		HTTP://wlfraed.home.netcom.com/
	(Bestiaria Support Staff:		web-asst <at> bestiaria.com)
		HTTP://www.bestiaria.com/
--
http://mail.python.org/mailman/listinfo/python-list

Bruno Desthuilliers | 28 Aug 12:28

Re: filter in for loop

GHZ a écrit :
> I would like to say something like:
> 
> for x in l if <expression>:
>     <do something>
> 
> e.g.
> 
> for filename in os.listdir(DIR) if filename[-4:] == '.xml':
>     <do something>
> 
> 
> instead of having to say:
> 
> for filename in os.listdir(DIR):
>     if filename[-4:] == '.xml':
>         <do something>
> 
> or
> 
> for filename in (f for f in os.listdir(DIR) if f[-4] == '.xml'):
>     <do something>
> 
> 
> is there a shortcut I'm missing?

Not AFAIK, and I sometimes regret it - but the first alternative is good 
enough IMHO.

<ot>
(Continue reading)

Chris | 28 Aug 13:10

Re: filter in for loop

On Aug 28, 12:20 pm, GHZ <geraint.willi...@gmail.com> wrote:
> I would like to say something like:
>
> for x in l if <expression>:
>     <do something>
>
> e.g.
>
> for filename in os.listdir(DIR) if filename[-4:] == '.xml':
>     <do something>
>
> instead of having to say:
>
> for filename in os.listdir(DIR):
>     if filename[-4:] == '.xml':
>         <do something>
>
> or
>
> for filename in (f for f in os.listdir(DIR) if f[-4] == '.xml'):
>     <do something>
>
> is there a shortcut I'm missing?

from glob import glob
from os import path
DIR = 'PathToFiles'
EXT = '*.xml'

for filename in glob(path.join(DIR, EXT)):
(Continue reading)

Timo | 28 Aug 13:12

Re: filter in for loop

On 28 elo, 13:20, GHZ <geraint.willi...@gmail.com> wrote:

>
> is there a shortcut I'm missing?

(Adding to Bruno's comment)

This kind of style provides a reusable filter/generator-function and
as a bonus, the for-loop becomes easier to understand.

from os.path import abspath
from glob import iglob

def ipathnames(pattern):
    return (abspath(n) for n in iglob(pattern))

for xmlfile in ipathnames('*.xml'):
    <do something>

-- timo
--
http://mail.python.org/mailman/listinfo/python-list


Gmane