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


Groups > comp.lang.python > #51144

Re: Converting a list of lists to a single list

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Converting a list of lists to a single list
Date 2013-07-24 11:56 -0400
References <ee6f5c48-6cb1-498f-813a-be2eef5411fc@googlegroups.com> <ksn22f$e97$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.5047.1374681415.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 7/23/2013 7:02 PM, Terry Reedy wrote:
> 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.

What you want is a flattened product with unchanged components of the 
successive products omitted in the flattening. The omission is the 
difficulty.

> A recursive generator suffices.

But see below for how to use itertools.product.

>> I want to convert an arbitrary number of lists with an arbitrary
>> number of elements in each list into a single list as follows.

While others answered the Python2-oriented question ("How do I produce a 
list from a list of lists"), I answered the Python-3 oriented question 
of how to produce an iterator from an iterable of iterables. This scales 
better to an input with lots of long sequences. There is usually no need 
to manifest the output as a list, as the typical use of the list will be 
to iterate it.

> 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

Here is filtered flattened product version. I think it clumsier than 
directly producing the items wanted, but it is good to know of this 
approach as a backup.

from itertools import product

def flatprod(iofi):  # iterable of iterables
     lofi = list(iofi)
     now = [object()] * len(lofi)
     for new in product(*lofi):
         i = 0
         while now[i] == new[i]:
             i += 1
         yield from new[i:]
         now = new

cfLL = list(flatprod(LL))

Same assert as before 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