NEWB: General purpose list iteration?

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

    #1

    NEWB: General purpose list iteration?

    I'm a real Python NEWB and am intrigued by some of Python's features, so I'm
    starting to write code to do some things to see how it works. So far I
    really like the lists and dictionaries since I learned to love content
    addressability in MATLAB. I was wondering it there's a simple routine (I
    think I can write a recurisve routine to do this.) to scan all the elements
    of a list, descending to lowest level and change something. What I'd like to
    do today is to convert everything from string to float. So, if I had a list
    of lists that looked like:
    [['1.1', '1.2', '1.3'], [['2.1', '2.2'], [['3.1', '3.2'], ['4.1', '4.2']]]]
    and I'd like it to be:
    [[1.1, 1.2, 1.3], [[2.1, 2.2], [[3.1, 3,2], [4.1, 4.2]]]]
    is there just a library routine I can call to do this? Right now, I'm using
    'for' loops nested to the maximum depth anticipated.

    for i in range(len(list) ):
    for j in range(len(list[i])):
    for k in range(len(list[i][j])):
    etc
    list[i][j][...] = float(list[i][j][....])

    Which works but is not pretty. I was just looking for a way to say:
    listb = float(lista)
    However, the way I've started jamming everything into lists and
    dictionaries, I'm sure I'll be needing to do other in-place conversions
    similar to this in the future.

    --
    Donald Newcomb
    DRNewcomb (at) attglobal (dot) net


  • Devan L

    #2
    Re: NEWB: General purpose list iteration?

    def descend(iterabl e):
    if hasattr(iterabl e, '__iter__'):
    for element in iterable:
    descend(element )
    else:
    do_something(it erable)

    This will just do_something(ob ject) to anything that is not an
    iterable. Only use it if all of your nested structures are of the same
    depth.

    If you used it on the following list of lists

    [[something],[[something_else],[other_thing]]]

    it would modify something, something_else, and other_thing.

    Comment

    • Peter Otten

      #3
      Re: NEWB: General purpose list iteration?

      Donald Newcomb wrote:
      [color=blue]
      > I was wondering it there's a simple routine (I
      > think I can write a recurisve routine to do this.) to scan all the
      > elements of a list, descending to lowest level and change something. What
      > I'd like to do today is to convert everything from string to float. So, if
      > I had a list of lists that looked like:
      > [['1.1', '1.2', '1.3'], [['2.1', '2.2'], [['3.1', '3.2'], ['4.1',
      > [['4.2']]]]
      > and I'd like it to be:
      > [[1.1, 1.2, 1.3], [[2.1, 2.2], [[3.1, 3,2], [4.1, 4.2]]]]
      > is there just a library routine I can call to do this? Right now, I'm
      > using 'for' loops nested to the maximum depth anticipated.[/color]

      A non-recursive approach:

      def enumerate_ex(it ems):
      stack = [(enumerate(item s), items)]
      while stack:
      en, seq = stack[-1]
      for index, item in en:
      if isinstance(item , list):
      stack.append((e numerate(item), item))
      break
      yield index, seq[index], seq
      else:
      stack.pop()


      data = [['1.1', '1.2', '1.3'], [['2.1', '2.2'], [['3.1', '3.2'], ['4.1',
      '4.2']]]]

      for index, value, items in enumerate_ex(da ta):
      items[index] = float(value)

      # Now let's test the algorithm and our luck with float comparisons
      assert data == [[1.1, 1.2, 1.3], [[2.1, 2.2], [[3.1, 3.2], [4.1, 4.2]]]]

      Peter

      Comment

      • Donald Newcomb

        #4
        Re: NEWB: General purpose list iteration?


        "Devan L" <devlai@gmail.c om> wrote in message
        news:1123817879 .772095.6240@g4 4g2000cwa.googl egroups.com...[color=blue]
        > This will just do_something(ob ject) to anything that is not an
        > iterable. Only use it if all of your nested structures are of the same
        > depth.[/color]

        Cool! I'll try it.

        --
        Donald Newcomb
        DRNewcomb (at) attglobal (dot) net


        Comment

        • Donald Newcomb

          #5
          Re: NEWB: General purpose list iteration?


          "Peter Otten" <__peter__@web. de> wrote in message
          news:ddhh60$d8s $00$1@news.t-online.com...[color=blue]
          > A non-recursive approach:
          >
          > def enumerate_ex(it ems):
          > stack = [(enumerate(item s), items)]
          > while stack:
          > en, seq = stack[-1]
          > for index, item in en:
          > if isinstance(item , list):
          > stack.append((e numerate(item), item))
          > break
          > yield index, seq[index], seq
          > else:
          > stack.pop()[/color]

          It's going to take me a while to figure out exactly what that does but it
          sure does what I wanted it to.
          Thanks.

          --
          Donald Newcomb
          DRNewcomb (at) attglobal (dot) net


          Comment

          Working...