filter in for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GHZ

    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?
  • Bruno Desthuilliers

    #2
    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>
    I'd rather use os.path.splitex t or str.endswith than subscript, but YMMV.
    </ot>

    Comment

    • Chris

      #3
      Re: filter in for loop

      On Aug 28, 12:20 pm, GHZ <geraint.willi. ..@gmail.comwro te:
      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)):
      print filename

      Comment

      • Timo

        #4
        Re: filter in for loop

        On 28 elo, 13:20, GHZ <geraint.willi. ..@gmail.comwro te:
        >
        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(patt ern):
        return (abspath(n) for n in iglob(pattern))

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

        -- timo

        Comment

        • Jerry Hill

          #5
          Re: filter in for loop

          On Thu, Aug 28, 2008 at 6:20 AM, GHZ <geraint.willia ms@gmail.comwro te:
          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.ifilt er if you need to deal
          with arbitrary iterables. I think your first example of:

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

          Is the most readable though.

          --
          Jerry

          Comment

          • Matthew Woodcraft

            #6
            Re: filter in for loop

            GHZ <geraint.willia ms@gmail.comwri tes:
            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-

            Comment

            • Emile van Sebille

              #7
              Re: filter in for loop

              Jerry Hill wrote:
              On Thu, Aug 28, 2008 at 6:20 AM, GHZ <geraint.willia ms@gmail.comwro te:
              >is there a shortcut I'm missing?
              >
              import glob

              Emile

              Comment

              Working...