zip() or what?

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

    zip() or what?

    Hi all

    Many thanks to those that answered my questions about whitespace and ord()
    being reverse of chr(). As well as the 2 things I asked about I learned
    about 5 other useful things.

    This I am trying to flip an array around so that the "subscripts " happen
    in the opposite order and reading the docs I thought that zip() did this.
    So I tried it like this:

    x=[[0.1,0.2],[1.1,1.2],[2.1,2.2]]
    print zip(x)

    and what I got was (removing the .0000000001s):

    [([0.1, 0.2],), ([1.1, 1.2],), ([2.1, 2.2],)]

    which is just my original array with an extra useless level in it.
    What I really wanted was this:

    [[0.1,1.1,2.1],[0.2,1.2,2.2]]

    So my question is how do I do that easily?
    And what on earth is zip() doing?

    Alternatively, is there a construct to get x[*][i] if you know what I mean?

    Have fun

    Ray

  • Erik Max Francis

    #2
    Re: zip() or what?

    Ray Tomes wrote:
    [color=blue]
    > What I really wanted was this:
    >
    > [[0.1,1.1,2.1],[0.2,1.2,2.2]]
    >
    > So my question is how do I do that easily?[/color]

    You wanted

    zip(*x)
    [color=blue]
    > Alternatively, is there a construct to get x[*][i] if you know what I
    > mean?[/color]

    Probably

    [i[1] for i in x]

    or

    map(lambda i: i[1], x)

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ What would physics look like without gravitation?
    \__/ Albert Einstein

    Comment

    • Ray Tomes

      #3
      Re: zip() or what?

      Erik Max Francis wrote:[color=blue]
      > Ray Tomes wrote:[color=green]
      >>request to flip array ...[/color][/color]
      [color=blue]
      > zip(*x)[/color]
      [color=blue][color=green]
      >>Alternatively , is there a construct to get x[*][i] if you know what I
      >>mean?[/color][/color]
      [color=blue]
      > [i[1] for i in x][/color]

      Thanks Erik, these do just what I want.
      I can understand the 2nd one, but I don't get the meaning of the * in the
      first. Is this like the opposite of putting [] around something or what?
      Under what circumstances can an * be used like this, and what is it
      called? - I don't know how to look for it in the docs :-)

      also, ...

      achrist@easystr eet.com wrote:[color=blue]
      > Ray Tomes wrote:[color=green]
      >>This I am trying to flip an array around so that the "subscripts " happen
      >>in the opposite order[/color][/color]
      [color=blue]
      > [x[-i-1] for i in range(len(x))][/color]

      Thanks Al, but that was not the flip I was looking for sorry - I hadn't
      realised it could be taken another way. I wanted to swap the subscripts
      with each other (a 45 degree reflection) not within one subscript end to
      end (a 90 degree reflection). Erik has done the one I wanted.

      Comment

      • Ray Tomes

        #4
        Re: zip() or what?

        Erik Max Francis wrote:[color=blue]
        > Ray Tomes wrote:[color=green]
        >>request to flip array ...[/color][/color]
        [color=blue]
        > zip(*x)[/color]
        [color=blue][color=green]
        >>Alternatively , is there a construct to get x[*][i] if you know what I
        >>mean?[/color][/color]
        [color=blue]
        > [i[1] for i in x][/color]

        Thanks Erik, these do just what I want.
        I can understand the 2nd one, but I don't get the meaning of the * in the
        first. Is this like the opposite of putting [] around something or what?
        Under what circumstances can an * be used like this, and what is it
        called? - I don't know how to look for it in the docs :-)

        also, ...

        achrist@easystr eet.com wrote:[color=blue]
        > Ray Tomes wrote:[color=green]
        >>This I am trying to flip an array around so that the "subscripts " happen
        >>in the opposite order[/color][/color]
        [color=blue]
        > [x[-i-1] for i in range(len(x))][/color]

        Thanks Al, but that was not the flip I was looking for sorry - I hadn't
        realised it could be taken another way. I wanted to swap the subscripts
        with each other (a 45 degree reflection) not within one subscript end to
        end (a 90 degree reflection). Erik has done the one I wanted.

        Comment

        • Erik Max Francis

          #5
          Re: zip() or what?

          Ray Tomes wrote:
          [color=blue]
          > I can understand the 2nd one, but I don't get the meaning of the * in
          > the
          > first. Is this like the opposite of putting [] around something or
          > what?
          > Under what circumstances can an * be used like this, and what is it
          > called? - I don't know how to look for it in the docs :-)[/color]

          f(x) calls the function f with the single argument x. f(*x) calls f
          with the arguments x, which is expected to be a sequence. The * syntax
          comes from defining functions, where a formal argument preceded by *
          means, "All the rest of the arguments as a tuple." So:
          [color=blue][color=green][color=darkred]
          >>> def f(*x): print x[/color][/color][/color]
          ....[color=blue][color=green][color=darkred]
          >>> s = [1, 2, 3]
          >>> f(s)[/color][/color][/color]
          ([1, 2, 3],)[color=blue][color=green][color=darkred]
          >>> f(*s)[/color][/color][/color]
          (1, 2, 3)

          The old way of writing the function call f(*x) was apply(f, x).

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
          / \ War is like love, it always finds a way.
          \__/ Bertolt Brecht

          Comment

          Working...