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


Groups > comp.lang.python > #51109

Re: Converting a list of lists to a single list

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
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.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'follows.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'jan': 0.12; 'first:': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'skip:[ 40': 0.16; 'subject:Converting': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'passes': 0.24; 'looks': 0.24; 'this:': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'assert': 0.31; 'lists': 0.32; 'figure': 0.32; 'convert': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'yield': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:a 30': 0.61; 'received:173': 0.61; 'first': 0.61; '102': 0.84; 'email name:steve': 0.84; 'how.': 0.84; 'received:fios.verizon.net': 0.84; 'lists:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Converting a list of lists to a single list
Date Tue, 23 Jul 2013 19:02:44 -0400
References <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7
In-Reply-To <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5019.1374620579.3114.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374620579 news.xs4all.nl 15945 [2001:888:2000:d::a6]:49088
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51109

Show key headers only | View raw


On 7/23/2013 5:52 PM, steve@divillo.com wrote:
> I think that itertools may be able to do what I want but I have not
> been able to figure out how.

A recursive generator suffices.

> I want to convert an arbitrary number of lists with an arbitrary
> number of elements in each list into a single list as follows.
>
> Say I have three lists:
>
> [[A0,A1,A2], [B0,B1,B2] [C0,C1,C2]]
>
> I would like to convert those to a single list that looks like this:
>
> [A0,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2,
 >  A1,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2,
 >  A2,B0,C0,C1,C2,B1,C0,C1,C2,B2,C0,C1,C2]

def crossflat(lofl):
     if lofl:
         first = lofl.pop(0)
         for o in first:
            yield o
            yield from crossflat(lofl.copy())

A0, A1, A2 = 100, 101, 102
B0, B1, B2 = 10, 11, 12
C0, C1, C2 = 0, 1, 2
LL = [[A0, A1, A2], [B0, B1, B2], [C0, C1, C2]]
cfLL = list(crossflat(LL))
print(cfLL)
assert cfLL == [
    A0, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2,
    A1, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2,
    A2, B0, C0, C1, C2, B1, C0, C1, C2, B2, C0, C1, C2]

passes

-- 
Terry Jan Reedy

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


Thread

Converting a list of lists to a single list steve@divillo.com - 2013-07-23 14:52 -0700
  Re: Converting a list of lists to a single list Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2013-07-24 00:34 +0200
  Re: Converting a list of lists to a single list MRAB <python@mrabarnett.plus.com> - 2013-07-23 23:52 +0100
  Re: Converting a list of lists to a single list Zero Piraeus <schesis@gmail.com> - 2013-07-23 18:49 -0400
  Re: Converting a list of lists to a single list Terry Reedy <tjreedy@udel.edu> - 2013-07-23 19:02 -0400
  Re: Converting a list of lists to a single list Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-07-23 20:28 -0400
  Re: Converting a list of lists to a single list Chris Angelico <rosuav@gmail.com> - 2013-07-24 15:40 +1000
  Re: Converting a list of lists to a single list Terry Reedy <tjreedy@udel.edu> - 2013-07-24 11:56 -0400
  Re: Converting a list of lists to a single list steve@divillo.com - 2013-07-24 11:11 -0700

csiph-web