iterators and views of lists

Discussion in 'Python' started by Brendan Miller, Dec 16, 2009.

  1. I was trying to reimplement some of the c++ library of generic
    algorithms in c++ in python, but I was finding that this is
    problematic to do this in a generic way because there isn't any
    equivalent of c++'s forward iterators, random access iterators, etc.
    i.e. all python iterators are just input iterators that can't mutate
    the sequence they iterate over nor move backwards or by an arbitrary
    offset.

    I'm wondering if anyone has done work towards creating more powerful
    iterators for python, or creating some more pythonic equivalent.

    In particular, I was thinking that slices are almost equivalent to a
    range of random access iterators, except that they create an
    unnecessary copy. If there was a version of slice that defined a
    mutable view over the original list, you'd have something equivalent
    to t a pair of random access iterators.

    For instance, if you wanted to reverse a segment of a list in place
    you would be able to do:

    list = [1,2,3,4]

    #pretend this is some variant of slice that produces views instead
    #obviously this couldn't use the same syntax, and might be a method instead.
    view = list[1:]
    view.reverse()

    print list
    [1,4,3,2]

    This doesn't really handle forward and bidirectional iterators...
    which I guess would be good for algorithms that operator over disk
    base datastructures...

    Anyone else had similar thoughts?

    Brendan
    Brendan Miller, Dec 16, 2009
    #1
    1. Advertising

  2. On Dec 16, 10:39 am, Brendan Miller <> wrote:
    > I was trying to reimplement some of the c++ library of generic
    > algorithms in c++ in python, but I was finding that this is
    > problematic to do this in a generic way because there isn't any
    > equivalent of c++'s forward iterators, random access iterators, etc.
    > i.e. all python iterators are just input iterators that can't mutate
    > the sequence they iterate over nor move backwards or by an arbitrary
    > offset.


    You might be interested in this library <http://pypi.python.org/pypi/
    stream>.

    You can easily create arbitrary "slice", for example

    i = mylist >> takei(primes())

    will return an iterator over the items of mylist with a prime number
    index, given that primes() return an iterator over prime numbers.

    I'm not familiar with forward/random-access/bidirectional iterators.
    Anh Hai Trinh, Dec 16, 2009
    #2
    1. Advertising

  3. On Dec 16, 1:58 pm, Anh Hai Trinh <> wrote:

    >
    > You might be interested in this library <http://pypi.python.org/pypi/
    > stream>.
    >
    > You can easily create arbitrary "slice", for example
    >
    >   i = mylist >> takei(primes())
    >
    > will return an iterator over the items of mylist with a prime number
    > index, given that primes() return an iterator over prime numbers.
    >



    Nice :)

    I was starting to experiment data flow programming with python myself,
    although I'm just playing with it..
    I find the idea of data flow programming fascinatin, and wonder if it
    can be considered a general-purpose program paradigm.

    Ciao
    -----
    FB
    Francesco Bochicchio, Dec 16, 2009
    #3
  4. Brendan Miller

    Lie Ryan Guest

    On 12/17/2009 4:44 AM, Francesco Bochicchio wrote:
    > On Dec 16, 1:58 pm, Anh Hai Trinh<> wrote:
    >
    >>
    >> You might be interested in this library<http://pypi.python.org/pypi/
    >> stream>.
    >>
    >> You can easily create arbitrary "slice", for example
    >>
    >> i = mylist>> takei(primes())
    >>
    >> will return an iterator over the items of mylist with a prime number
    >> index, given that primes() return an iterator over prime numbers.
    >>

    >
    >
    > Nice :)
    >
    > I was starting to experiment data flow programming with python myself,
    > although I'm just playing with it..
    > I find the idea of data flow programming fascinatin, and wonder if it
    > can be considered a general-purpose program paradigm.


    It is actually. http://en.wikipedia.org/wiki/Pipeline_programming
    http://en.wikipedia.org/wiki/Flow-based_programming
    Lie Ryan, Dec 19, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    485
    Kai-Uwe Bux
    May 8, 2005
  2. Adelein and Jeremy
    Replies:
    3
    Views:
    327
    Duncan Booth
    Dec 3, 2003
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    401
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. , India
    Replies:
    10
    Views:
    1,075
    James Kanze
    Aug 8, 2009
  5. Brendan Miller

    Re: iterators and views of lists

    Brendan Miller, Dec 16, 2009, in forum: Python
    Replies:
    28
    Views:
    558
    Anh Hai Trinh
    Dec 20, 2009
Loading...

Share This Page