Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103533 > unrolled thread

Re: Cycling through iterables diagonally

Started byMark Lawrence <breamoreboy@yahoo.co.uk>
First post2016-02-26 11:00 +0000
Last post2016-02-26 11:00 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Cycling through iterables diagonally Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-26 11:00 +0000

#103533 — Re: Cycling through iterables diagonally

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-02-26 11:00 +0000
SubjectRe: Cycling through iterables diagonally
Message-ID<mailman.144.1456484470.20994.python-list@python.org>
On 26/02/2016 09:59, Peter Otten wrote:
> Pablo Lucena wrote:
>
>> Say I have a group of 4 lists as follows:
>>
>> l1 = ['a1', 'a2', 'a3', 'a4']
>> l2 = ['b1', 'b2', 'b3', 'b4']
>> l3 = ['c1', 'c2', 'c3', 'c4']
>> l4 = ['d1', 'd2', 'd3', 'd4']
>>
>> I would like to cycle through these lists "diagonally" in groups of
>> len(list) (in this example, each list has 4 items).
>
>> Prior to this I was mucking around with index counting while looping, and
>> popping lists out of a deque, popping an item out of the list, and
>> appending the list back into the deque during each iteration.
>>
>> Is there a better/cleaner way to do this? I was hoping for some cool
>> itertools logic =)
>
> I have a weak spot for the itertools myself, but I think in terms of clarity
> it is hard to beat the conventional
>
> def diagonals(a):
>      N = len(a)
>      for i in range(N):
>          for k in range(N):
>              yield a[k][(k+i)%N]
>
> print(list(diagonals([l1, l2, l3, l4])))
>
> Of course that's as uncool as it can get ;)
>

It might be uncool, but at least I can read it, unlike the earlier Jussi 
Piitulainen answer that made my head spin :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web