Hacker News new | past | comments | ask | show | jobs | submit login
zip is its own inverse
8 points by emmett on Jan 30, 2008 | hide | past | favorite | 5 comments
I found myself, for fairly silly reasons, desiring to find the inverse to zip() today. After some thought, I realized zip is its own inverse:

  >>> def doublezip(xs):
  ...   print zip(*zip(*xs))
  ... 
  >>> doublezip([[9, 8, 7, 6], [1, 2, 3, 4], [4, 5, 6, 7]])
  [(9, 8, 7, 6), (1, 2, 3, 4), (4, 5, 6, 7)]
This struck me very interesting. I'm not sure I could ever explain why though.



That's kinda interesting, though if you just work it out on paper, you can see why.

zip basically groups all the elements with the same index together (like a dot product, except you don't multiply or sum). So all the elements from the first array will be in the array of the first resulting zip.

  zip([a0, a1,..,an], [b0, b1,..,bn]) =>
    [[a0, b0], [a1, b1], ..., [an, bn]]
And if you look at it, of all the resulting arrays of the first zip, all the ones in the first index of every array came from the first array. all the ones in the 2nd index of every array came from the 2nd array. If you run a 2nd zip, you're just, once again, collecting all the elements of all arrays with the same index, which reverts it back to what it was before.

Looking at it another way, if you think about it as a matrix, you'd have, say a bunch of row vectors:

  [[1,2,3,4],
   [5,6,7,8]]
If you run the first zip, you'd just get a bunch of column vectors (excuse the ascii, can't realy make column vectors:

  [[1  [2  [3  [4
    5], 6], 7]  8]]
And if you run zip again, you just get back row vectors.


Aha!

zip == matrix transpose.

I wonder why I never noticed that before...


yeah, I never noticed it until you posted this and I worked it out on paper.

when I first saw zip, I thought, when would I use that? Then when I was playing around with erlang trying to write a dot product function, that's when I figured it out.


Zip = Matrix Transpose is on Peter Norvig's list of Infrequently Answered Questions about Python.


zip is matrix transposition in disguise.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: