Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103530
| Path | csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Pablo Lucena <plucena24@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Cycling through iterables diagonally |
| Date | Thu, 25 Feb 2016 23:44:05 -0800 |
| Lines | 70 |
| Message-ID | <mailman.142.1456472649.20994.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de GXn2ShLuS8k/XXtUrwtbYQl8bqJwjhunSFnqlajGyoQA== |
| Return-Path | <plucena24@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.014 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'counting': 0.07; 'collections': 0.09; 'logic': 0.09; 'received:209.85.218': 0.10; 'index': 0.13; 'deque': 0.16; 'deque,': 0.16; 'iteration.': 0.16; 'itertools': 0.16; 'popping': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; '(in': 0.18; 'trying': 0.22; '8bit%:5': 0.23; 'import': 0.24; 'message-id:@mail.gmail.com': 0.27; 'cool': 0.27; 'appending': 0.29; 'group:': 0.29; 'thanks!': 0.34; 'this?': 0.34; 'lists': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'follows:': 0.35; 'item': 0.35; 'list,': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'say': 0.37; 'thought': 0.37; 'doing': 0.38; 'received:209': 0.38; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'hello,': 0.40; 'some': 0.40; 'back': 0.62; '8bit%:46': 0.76; 'hoping': 0.77; 'groups:': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=oHU1a129VqpVOg8zy7t2fDYVX71wkoV99nyhOWJVvDM=; b=l4FxhKKlQZFwTHSVCw0pYDb03WS/YSDdo6Z0A5W8AfqCdfkCeAoBfptt0na5dawkVk oagIvWw4OCveJNqS56No2EjRFP2Sth7cCeCRzKkPqiQovCyBc9vK7CgqiAltnCsKHF8X VHTABYe/r72eVPI0BnRSZrKqRZ05GN1vFPoi88KgAiTf3lR4r8lxokbcvF7HB8w/+wyu DhY9CP57RvC1G/v8hZdIrU6/LG5T8bBHpOzp6tIojrYwT+kQiZeqolgolN5N87wrsl3V Ab0jQkzxD5Fby9S1JvG4v7ZkP7dXQLNBpPNtxalA3fXHuyzqcI3TT/nw0vfm85LgL8FZ Ft9g== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=oHU1a129VqpVOg8zy7t2fDYVX71wkoV99nyhOWJVvDM=; b=WCcZEe9ZHKCWj216VJlMt96QlJZWm/H26tz+URMXtK5Z6CrAMxdEuyuxXKHqUBlyPm rXs8jwBSAowOwYBKuhmQHiIfNVANCPdTuPba+QdfORY4cl2DQE5OniSPlKbi9PAOl3BG y9UY+DaLjn3HdrKNWSHUzrMIRfa7kvrhltrquA0yj7SangyKIW7PXbCXfzp83xeQTuPC 4N6EVMJ+u/puAZTGek/PjLFLlNM6MTvFKEMUq50smzQhjXN0t3R9vvCBy5ckABKO7h/o mCI+9Pzoif7yiNeDCUylOhjZ9gdH0cnNQipD51RnOCxvVWDwkbX1WB2jali7FgCEuvHr nVQg== |
| X-Gm-Message-State | AD7BkJJ55FwFpMoHqh7jj67OR3TYEClNG2wYXXHkujoMm/PSouxIHrDeYFfUlWwumHK1aqUQUc5Hx6RpOd4caw== |
| X-Received | by 10.202.182.11 with SMTP id g11mr62627oif.133.1456472646008; Thu, 25 Feb 2016 23:44:06 -0800 (PST) |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.21rc2 |
| 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:103530 |
Show key headers only | View raw
Hello, I am trying to accomplish the following: 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). cycle1: a1, b2, b3, b4 cycle2: a2, b3, c4, d1 cycle3: a3, b4, c1, d2 cycle4: a4, b1, c2, d3 The way I thought about doing this is as follows: from collections import deque from itertools import cycle l1 = deque(['a1', 'a2', 'a3', 'a4']) l2 = deque(['b1', 'b2', 'b3', 'b4']) l3 = deque(['c1', 'c2', 'c3', 'c4']) l4 = deque(['d1', 'd2', 'd3', 'd4']) l1.rotate(-0) l2.rotate(-1) l3.rotate(-2) l4.rotate(-3) groups = cycle([l1, l2, l3, l4]) In [115]: for group in groups: .....: if not group: .....: break .....: print(group.popleft()) .....: a1 b2 c3 d4 a2 b3 c4 d1 a3 b4 c1 d2 a4 b1 c2 d3 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 =) Thanks! -- *Pablo*
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Cycling through iterables diagonally Pablo Lucena <plucena24@gmail.com> - 2016-02-25 23:44 -0800 Re: Cycling through iterables diagonally Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-02-26 11:21 +0200 Re: Cycling through iterables diagonally marco.nawijn@colosso.nl - 2016-02-26 06:27 -0800
csiph-web