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

From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Converting a list of lists to a single list
Date 2013-07-23 20:28 -0400
Organization IISS Elusive Unicorn
References <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com> <51EF04F7.9040401@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5023.1374625711.3114.python-list@python.org> (permalink)

Show all headers | 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