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


Groups > comp.lang.python > #51113

Re: Converting a list of lists to a single list

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'element': 0.07; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'repeated': 0.09; 'def': 0.12; '>in': 0.16; 'escribi\xf3:': 0.16; 'itertools': 0.16; 'lists...': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'res': 0.16; 'subject:Converting': 0.16; 'wed,': 0.18; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'url:home': 0.24; 'looks': 0.24; 'this:': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'subject:list': 0.30; 'lists': 0.32; 'third': 0.33; 'case,': 0.35; 'convert': 0.35; 'no,': 0.35; 'subject:lists': 0.35; '+0200,': 0.36; 'shows': 0.36; 'list': 0.37; 'received:76': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'first': 0.61; 'skip:r 30': 0.69; 'jul': 0.74; 'email name:steve': 0.84; 'rafael': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Converting a list of lists to a single list
Date Tue, 23 Jul 2013 20:28:21 -0400
Organization IISS Elusive Unicorn
References <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com> <51EF04F7.9040401@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host adsl-76-249-27-96.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 6.00/32.1186
X-No-Archive YES
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.5023.1374625711.3114.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374625711 news.xs4all.nl 15878 [2001:888:2000:d::a6]:38205
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51113

Show key headers only | View raw


On Wed, 24 Jul 2013 00:34:31 +0200, Rafael Durán Castañeda
<rafadurancastaneda@gmail.com> declaimed the following:

>El 23/07/13 23:52, steve@divillo.com escribió:
>> [[A0,A1,A2], [B0,B1,B2] [C0,C1,C2]]
>Hi,
>
>I think you are looking for itertools.chain, or in this case, 
>itertools.chain.from_iterable:
>
>In [1]: x = [['A0','A1','A2'], ['B0','B1','B2'], ['C0','C1','C2']]
>
>In [2]: import itertools
>
>In [3]: [ y for y in itertools.chain.from_iterable(x)]
>Out[3]: ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C0', 'C1', 'C2']
>
	No, that just flattened the lists... The OP example shows the third
list is repeated after each element of the second list, and the flattened
result of that is then repeated for each element of the first list (and
flattened).
op> I would like to convert those to a single list that looks like this:
op> 
op>
[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]
op> 

-=-=-=-=-=-

inp = [ ["%s%s" % (a,o) for o in "012"] for a in "ABC"]
print inp

def repFlatten(LoL):    #LoL List of Lists
    if not LoL or (type(LoL) != type([])
                   or type(LoL[0]) != type([])):
        return LoL
    res = []
    for itm in LoL[0]:
        res.append(itm)
        res.extend(repFlatten(LoL[1:]))
    return res

print repFlatten(inp)
-=-=-=-=-=-
[['A0', 'A1', 'A2'], ['B0', 'B1', 'B2'], ['C0', 'C1', 'C2']]

['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']
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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