a question about zip...

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

    #1

    a question about zip...

    I had a structure that looked like this
    ((0,1), (2, 3), (4, 5), (6,7)

    I changed my code slightly and now I do this:
    odd = (1,3,5,7)
    even = (0,2,4,6)
    all = zip(even, odd)

    however the zip produces:
    [(0, 1), (2, 3), (4, 5), (6, 7)]

    Which is a list of tuples.. I wanted a tuple of tuples...

  • Jan Niklas Fingerle

    #2
    Re: a question about zip...

    KraftDiner <bobrien18@yaho o.com> wrote:[color=blue]
    > [(0, 1), (2, 3), (4, 5), (6, 7)]
    >
    > Which is a list of tuples.. I wanted a tuple of tuples...[/color]
    [color=blue][color=green][color=darkred]
    >>> odd = (1,3,5,7)
    >>> even = (0,2,4,6)
    >>> all = zip(even, odd)
    >>> all[/color][/color][/color]
    [(0, 1), (2, 3), (4, 5), (6, 7)][color=blue][color=green][color=darkred]
    >>> tuple(all)[/color][/color][/color]
    ((0, 1), (2, 3), (4, 5), (6, 7))

    Cheers,
    --Jan Niklas

    Comment

    • Xavier Morel

      #3
      Re: a question about zip...

      KraftDiner wrote:[color=blue]
      > I had a structure that looked like this
      > ((0,1), (2, 3), (4, 5), (6,7)
      >
      > I changed my code slightly and now I do this:
      > odd = (1,3,5,7)
      > even = (0,2,4,6)
      > all = zip(even, odd)
      >
      > however the zip produces:
      > [(0, 1), (2, 3), (4, 5), (6, 7)]
      >
      > Which is a list of tuples.. I wanted a tuple of tuples...
      >[/color]
      tuple(zip(even, odd))

      and if you fear for memory efficiency

      tuple(iterator. izip(even, odd))

      Comment

      • Diez B. Roggisch

        #4
        Re: a question about zip...

        KraftDiner schrieb:[color=blue]
        > I had a structure that looked like this
        > ((0,1), (2, 3), (4, 5), (6,7)
        >
        > I changed my code slightly and now I do this:
        > odd = (1,3,5,7)
        > even = (0,2,4,6)
        > all = zip(even, odd)
        >
        > however the zip produces:
        > [(0, 1), (2, 3), (4, 5), (6, 7)]
        >
        > Which is a list of tuples.. I wanted a tuple of tuples...[/color]

        Use tuple(zip(...))

        There is a reason that zip returns a list: appending to a list is considered a "natural" operation, whereas extending an
        immutable(!) tuple creates a _new_ tuple for each entry. Which is a comparable costly operation - actually quadratic
        complexity instead of linear.

        So - if you need a tuple afterwards, convert it as shown above.

        Diez

        Comment

        • Steven D'Aprano

          #5
          Re: a question about zip...

          KraftDiner wrote:
          [color=blue]
          > I had a structure that looked like this
          > ((0,1), (2, 3), (4, 5), (6,7)
          >
          > I changed my code slightly and now I do this:
          > odd = (1,3,5,7)
          > even = (0,2,4,6)
          > all = zip(even, odd)
          >
          > however the zip produces:
          > [(0, 1), (2, 3), (4, 5), (6, 7)]
          >
          > Which is a list of tuples.. I wanted a tuple of tuples...[/color]

          Others have already told you how to do this. But I have
          a question: why do you care that it is a tuple, not a
          list? Is there something you need to do with it that
          can be done with tuples but not lists?

          In other words, while it isn't *wrong* to convert the
          list to a tuple, perhaps it is *unnecessary*?


          --
          Steven.

          Comment

          • Steve Holden

            #6
            Re: a question about zip...

            KraftDiner wrote:[color=blue]
            > I had a structure that looked like this
            > ((0,1), (2, 3), (4, 5), (6,7)
            >
            > I changed my code slightly and now I do this:
            > odd = (1,3,5,7)
            > even = (0,2,4,6)
            > all = zip(even, odd)
            >
            > however the zip produces:
            > [(0, 1), (2, 3), (4, 5), (6, 7)]
            >
            > Which is a list of tuples.. I wanted a tuple of tuples...
            >[/color]
            So apply tuple() to the list!

            all = tuple(zip(even, odd))

            regards
            Steve
            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd www.holdenweb.com
            Love me, love my blog holdenweb.blogs pot.com

            Comment

            Working...