4 Replies - 453 Views - Last Post: 26 November 2012 - 09:00 AM Rate Topic: -----

#1 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

zipping across a list of lists?

Posted 25 November 2012 - 11:44 PM

Anyone got a good idiomatic way to zip across a list of lists?

I can do
>>> l = [[1,2,3][4,5,6],[7,8,9]]
>>> zip (l[0],l[1],l[2])


and get

[[1,4,7], [2,5,8],3,6,9]]

but is there a way to do this without listing the sublists?

I have a list of lists of strings, and I'm trying to do some cross-matching across them. This would be useful.

This post has been edited by jon.kiparsky: 25 November 2012 - 11:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: zipping across a list of lists?

#2 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: zipping across a list of lists?

Posted 26 November 2012 - 12:15 AM

>>> l = [[1,2,3],[4,5,6],[7,8,9]]
>>> exec(compile("a = zip("+",".join(["l[{0}]".format(x) for x in range(len(l))])+")", "<string>", "exec"))
>>> a
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]



Best I can do before work tomorrow. Would be nice to see what sort of things you're dealing with where zipping a list of lists would be useful.

Remember to change the name of the list in the formatted string if it's not actually l in your code.

EDIT: Especially if Knuth had anything to do with it.

This post has been edited by Python_4_President: 26 November 2012 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: zipping across a list of lists?

Posted 26 November 2012 - 12:31 AM

Quote

Would be nice to see what sort of things you're dealing with where zipping a list of lists would be useful.


It's actually a list of lists of strings. I have a set of texts encrypted with the same one-time pad. The task is to extract the content. So we start by xor-ing all of the messages against each other, producing a matrix (symmetrical along the diagonal) of cross-encrypted strings.
(str1 ^ key) ^ (str2 ^ key) == (str1 ^ str2), so now we have a list of plaintexts xor-ed against each other. Among other things, colliding bytes show up as 0x00, and collisions of alpha characters against 0x20 show up as toggled-case characters ('r' ^ spc == 'R'). So you can get some information by comparing across columns.

I'm well aware that there are easier ways to do this but I'm not seeing ways to make them work, so I'm doing it the hard way, hoping that I might get smarter if I bang my head against the wall long enough.

EDIT: Essentially, this would mean turning a list of lists into a proper matrix, which would be cool

This post has been edited by jon.kiparsky: 26 November 2012 - 12:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: zipping across a list of lists?

Posted 26 November 2012 - 04:42 AM

zip(*l)

Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: zipping across a list of lists?

Posted 26 November 2012 - 09:00 AM

You know, I've even used that before. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1