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


Groups > comp.lang.python > #103533

Re: Cycling through iterables diagonally

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Newsgroups comp.lang.python
Subject Re: Cycling through iterables diagonally
Date Fri, 26 Feb 2016 11:00:31 +0000
Lines 43
Message-ID <mailman.144.1456484470.20994.python-list@python.org> (permalink)
References <CAB_tDZwN8Onys+GLQme2cwKO2Fdo68b-erBpzky7Lv+r0ANNtA@mail.gmail.com> <nap7md$ktd$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de sKyI2eLFZtNC8uRQn99p2AcWchXRbVOV/EpJ58paxUHg==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'counting': 0.07; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'index': 0.13; 'def': 0.13; 'deque': 0.16; 'deque,': 0.16; 'iteration.': 0.16; 'itertools': 0.16; 'popping': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '(in': 0.18; 'language': 0.19; 'lawrence': 0.22; 'unlike': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'earlier': 0.27; 'least': 0.27; 'cool': 0.27; 'yield': 0.27; 'appending': 0.29; 'weak': 0.29; 'language.': 0.32; 'this?': 0.34; 'lists': 0.34; 'list': 0.34; 'follows:': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'mark': 0.40; 'some': 0.40; 'back': 0.62; 'charset:windows-1252': 0.62; 'course': 0.62; 'spot': 0.63; 'our': 0.64; 'beat': 0.66; 'hoping': 0.77; 'otten': 0.84; 'pablo': 0.84; 'pythonistas,': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 80.234.182.166
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0
In-Reply-To <nap7md$ktd$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103533

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web