How to iterate through a sequence, grabbing subsequences?

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

    #1

    How to iterate through a sequence, grabbing subsequences?

    I wrote a function that I suspect may already exist as a python builtin,
    but I can't find it:

    def chunkify(s, chunksize):
    "Yield sequence s in chunks of size chunksize."
    for i in range(0, len(s), chunksize):
    yield s[i:i+chunksize]

    I wrote this because I need to take a string of a really, really long
    length and process 4000 bytes at a time.

    Is there a better solution?

    Matt

    --
    A better way of running series of SAS programs:

  • Bruno Desthuilliers

    #2
    Re: How to iterate through a sequence, grabbing subsequences?

    Matthew Wilson wrote:
    I wrote a function that I suspect may already exist as a python builtin,
    but I can't find it:
    >
    def chunkify(s, chunksize):
    "Yield sequence s in chunks of size chunksize."
    for i in range(0, len(s), chunksize):
    yield s[i:i+chunksize]
    >
    I wrote this because I need to take a string of a really, really long
    length and process 4000 bytes at a time.
    >
    Is there a better solution?
    I don't know if it's better, but StringIO let you read a string as if it
    was a file:

    def chunkify(s, chunksize):
    f = StringIO.String IO(long_string)
    chunk = f.read(chunksiz e)
    while chunk:
    yield chunk
    chunk = f.read(chunksiz e)
    f.close()

    Now I'm sure someone will come up with a solution that's both far better
    and much more obvious (at least if you're Dutch <g>)

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Tim Chase

      #3
      Re: How to iterate through a sequence, grabbing subsequences?

      def chunkify(s, chunksize):
      "Yield sequence s in chunks of size chunksize."
      for i in range(0, len(s), chunksize):
      yield s[i:i+chunksize]
      >
      I wrote this because I need to take a string of a really, really long
      length and process 4000 bytes at a time.
      >
      Is there a better solution?
      My first thought, if len(s) is truely huge, would be to replace
      range() with xrange() so that you don't build a list of
      len(s)/chunksize elements, just to throw it away.

      However, I think that's about as good as this common idiom gets.

      I've seen variants which will always yield portions of chunksize
      in size, padding it out to the proper length, but that's a
      specification issue that you don't seem to want/need.

      -tkc




      Comment

      • Fredrik Lundh

        #4
        Re: How to iterate through a sequence, grabbing subsequences?

        Matthew Wilson wrote:
        I wrote a function that I suspect may already exist as a python builtin,
        but I can't find it:
        >
        def chunkify(s, chunksize):
        "Yield sequence s in chunks of size chunksize."
        for i in range(0, len(s), chunksize):
        yield s[i:i+chunksize]
        >
        I wrote this because I need to take a string of a really, really long
        length and process 4000 bytes at a time.
        >
        Is there a better solution?
        what's wrong with your solution ?

        </F>

        Comment

        • George Sakkis

          #5
          Re: How to iterate through a sequence, grabbing subsequences?

          Matthew Wilson wrote:
          I wrote a function that I suspect may already exist as a python builtin,
          but I can't find it:
          >
          def chunkify(s, chunksize):
          "Yield sequence s in chunks of size chunksize."
          for i in range(0, len(s), chunksize):
          yield s[i:i+chunksize]
          >
          I wrote this because I need to take a string of a really, really long
          length and process 4000 bytes at a time.
          >
          Is there a better solution?
          There's not any builtin for this, but the same topic came up just three
          days ago: http://tinyurl.com/qec2p.

          Regards,
          George

          Comment

          Working...