Re: flattening lists

2022-10-12 Thread Antoon Pardon
Op 11/10/2022 om 21:32 schreef SquidBits _: Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g. [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. I have had to flatten lists quite

Re: flattening lists

2022-10-11 Thread Dan Stromberg
On Tue, Oct 11, 2022 at 12:48 PM SquidBits _ wrote: > Does anyone else think there should be a flatten () function, which just > turns a multi-dimensional list into a one-dimensional list in the order > it's in. e.g. > > [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. > > I have had to fla

Re: flattening lists

2022-10-11 Thread MRAB
On 2022-10-11 21:09, Stefan Ram wrote: r...@zedat.fu-berlin.de (Stefan Ram) writes: . I never understood "yield from" until just now, when I was thinking, "Maybe this could be the piece that fits in here!" PS: If I'm starting to think about it: Having succeeded after using it by trial in

Re: flattening lists

2022-10-11 Thread dn
On 12/10/2022 08.32, SquidBits _ wrote: Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g. [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. I have had to flatten lists quite a few

Re: flattening lists

2022-10-11 Thread Thomas Passin
Is this what you usually do? l1 = [[1,2,3],[4,5,6,7],[8,9]] l2 = [] for lz in l1: l2.extend(lz) print(l2) # [1,2,3,4,5,6,7,8,9] Not all that "tedious", perhaps... I tend to accumulate little utilities like this in a file and point to it with a .pth file. Of course, you have to remember

Re: flattening lists

2022-10-11 Thread David Lowry-Duda
On Tue, Oct 11, 2022 at 12:32:23PM -0700, SquidBits _ wrote: Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g. [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. I have had to flatt

Re: flattening lists

2022-10-11 Thread Larry Martell
On Tue, Oct 11, 2022 at 12:48 PM SquidBits _ wrote: > > Does anyone else think there should be a flatten () function, which just > turns a multi-dimensional list into a one-dimensional list in the order it's > in. e.g. > > [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. > > I have had to

flattening lists

2022-10-11 Thread SquidBits _
Does anyone else think there should be a flatten () function, which just turns a multi-dimensional list into a one-dimensional list in the order it's in. e.g. [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9]. I have had to flatten lists quite a few times and it's quite tedious to type out.

Re: Flattening lists

2009-02-09 Thread ptn
On Feb 5, 2:07 pm, rdmur...@bitdance.com wrote: > Quoth J Kenneth King : > > > > > mk writes: > > > > Hello everybody, > > > > Any better solution than this? > > > > def flatten(x): > > >     res = [] > > >     for el in x: > > >         if isinstance(el,list): > > >             res.extend(flatten

Re: Flattening lists

2009-02-08 Thread Rhamphoryncus
On Feb 7, 3:07 pm, wrote: > On Sat, 7 Feb 2009 12:50:22 -0800 (PST) > Rhamphoryncus wrote: > > Can you explain this in a little more detail? > > In the application, there is one main numpy array of type "O". > Each element of the array corresponds to one cell in a grid. The user > may enter a Pyt

Re: Flattening lists

2009-02-07 Thread mmanns
On Sat, 7 Feb 2009 12:50:22 -0800 (PST) Rhamphoryncus wrote: > On Feb 7, 1:39 pm, wrote: > > On Sat, 7 Feb 2009 01:06:06 -0800 (PST) > > Rhamphoryncus wrote: > > > > > What usecase do you have for such inconsistently structured data? > > > > I have a similar use case in pyspread, which is a Pyt

Re: Flattening lists

2009-02-07 Thread Rhamphoryncus
On Feb 7, 1:39 pm, wrote: > On Sat, 7 Feb 2009 01:06:06 -0800 (PST) > Rhamphoryncus wrote: > > > What usecase do you have for such inconsistently structured data? > > I have a similar use case in pyspread, which is a Python spreadsheet > that employs numpy object arrays. Since the Python objects

Re: Flattening lists

2009-02-07 Thread mmanns
On Sat, 7 Feb 2009 01:06:06 -0800 (PST) Rhamphoryncus wrote: > On Feb 6, 10:21 pm, rdmur...@bitdance.com wrote: > > Quoth Mensanator : > > > def flatten(listOfLists): > > >     return list(chain.from_iterable(listOfLists)) > > > >     Python 2.6.1 (r261:67515, Jan  7 2009, 17:09:13) > >     [GCC

Re: Flattening lists

2009-02-07 Thread rdmurray
Rhamphoryncus wrote: > On Feb 6, 10:21=A0pm, rdmur...@bitdance.com wrote: > > Quoth Mensanator : > > > def flatten(listOfLists): > > > =A0 =A0 return list(chain.from_iterable(listOfLists)) > > > > =A0 =A0 Python 2.6.1 (r261:67515, Jan =A07 2009, 17:09:13) > > =A0 =A0 [GCC 4.3.2] on linux2 > > =A0

Re: Flattening lists

2009-02-07 Thread Rhamphoryncus
On Feb 6, 10:21 pm, rdmur...@bitdance.com wrote: > Quoth Mensanator : > > def flatten(listOfLists): > >     return list(chain.from_iterable(listOfLists)) > >     Python 2.6.1 (r261:67515, Jan  7 2009, 17:09:13) >     [GCC 4.3.2] on linux2 >     Type "help", "copyright", "credits" or "license" for m

Re: Flattening lists

2009-02-06 Thread rdmurray
Quoth Mensanator : > On Feb 6, 3:23=A0pm, Rhamphoryncus wrote: > > On Feb 5, 1:16=A0pm, Michele Simionato > > wrote: > > > > > On Feb 5, 7:24=A0pm, a...@pythoncraft.com (Aahz) wrote: > > > > In article > > > > , > > > > Michele Simionato =A0 wrote: > > > > >Looks fine to me. In some situations y

Re: Flattening lists

2009-02-06 Thread Michele Simionato
On Feb 6, 10:23 pm, Rhamphoryncus wrote: > On Feb 5, 1:16 pm, Michele Simionato > wrote: > > > On Feb 5, 7:24 pm, a...@pythoncraft.com (Aahz) wrote: > > > > In article > > > , > > > Michele Simionato   wrote: > > > > >Looks fine to me. In some situations you may also use hasattr(el, > > > >'__it

Re: Flattening lists

2009-02-06 Thread Mensanator
On Feb 6, 3:23 pm, Rhamphoryncus wrote: > On Feb 5, 1:16 pm, Michele Simionato > wrote: > > > On Feb 5, 7:24 pm, a...@pythoncraft.com (Aahz) wrote: > > > > In article > > > , > > > Michele Simionato   wrote: > > > > >Looks fine to me. In some situations you may also use hasattr(el, > > > >'__ite

Re: Flattening lists

2009-02-06 Thread Rhamphoryncus
On Feb 5, 1:16 pm, Michele Simionato wrote: > On Feb 5, 7:24 pm, a...@pythoncraft.com (Aahz) wrote: > > > In article > > , > > Michele Simionato   wrote: > > > >Looks fine to me. In some situations you may also use hasattr(el, > > >'__iter__') instead of isinstance(el, list) (it depends if you wa

Re: Flattening lists

2009-02-05 Thread Benjamin Peterson
mk gmail.com> writes: > Hmm, I'm surprised by even that! Apparently list creation is more > expensive than I thought - it seems somewhat more expensive than the > cost of interpreting bytecode for "if var is None". Either list creation > is somewhat costly, or "if var is None" is really cheap.

Re: Flattening lists

2009-02-05 Thread Michele Simionato
On Feb 5, 7:24 pm, a...@pythoncraft.com (Aahz) wrote: > In article > , > Michele Simionato   wrote: > > > > >Looks fine to me. In some situations you may also use hasattr(el, > >'__iter__') instead of isinstance(el, list) (it depends if you want to > >flatten generic iterables or only lists). > >

Re: Flattening lists

2009-02-05 Thread Tobiah
On Thu, 05 Feb 2009 11:06:39 -0800, Tobiah wrote: > >> Hello everybody, >> >> Any better solution than this? > > a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]] print str(a).replace('[', > '').replace(']', '').split(', ') > > ;) Or: a = ['text', 'string', 3, [4, 5, 6], [[7, 8], [9, 10]]] print e

Re: Flattening lists

2009-02-05 Thread Tobiah
> Hello everybody, > > Any better solution than this? a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]] print str(a).replace('[', '').replace(']', '').split(', ') ;) -- http://mail.python.org/mailman/listinfo/python-list

Re: Flattening lists

2009-02-05 Thread rdmurray
Quoth J Kenneth King : > mk writes: > > > Hello everybody, > > > > Any better solution than this? > > > > def flatten(x): > > res = [] > > for el in x: > > if isinstance(el,list): > > res.extend(flatten(el)) > > else: > > res.append(el) > > retu

Re: Flattening lists

2009-02-05 Thread Aahz
In article , Michele Simionato wrote: > >Looks fine to me. In some situations you may also use hasattr(el, >'__iter__') instead of isinstance(el, list) (it depends if you want to >flatten generic iterables or only lists). Of course, once you do that, you need to special-case strings... -- Aahz

Re: Flattening lists

2009-02-05 Thread rdmurray
Quoth rdmur...@bitdance.com: > This is all premature optimization, except for the goopy code, which is > presumably used enough to make it worth optimizing. And guess what? > The goopy code wins. What the people theorizing about the speed of > extend vs list creation miss is that the things with

Re: Flattening lists

2009-02-05 Thread Sion Arrowsmith
mk wrote: >Brian Allen Vanderburg II wrote: >> I think it may be just a 'little' more efficient to do this: >> >> def flatten(x, res=None): >>if res is None: >> res = [] >>for el in x: >> if isinstance(el, (tuple, list)): >> flatten(el, res) >> else: >>

Re: Flattening lists

2009-02-05 Thread J Kenneth King
mk writes: > Hello everybody, > > Any better solution than this? > > def flatten(x): > res = [] > for el in x: > if isinstance(el,list): > res.extend(flatten(el)) > else: > res.append(el) > return res > > a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10

Re: Flattening lists

2009-02-05 Thread rdmurray
Baolong zhen wrote: > On Thu, Feb 5, 2009 at 10:17 PM, mk wrote: > > > Brian Allen Vanderburg II wrote: > > >> def flatten(x): > > >> res = [] > > >> for el in x: > > >> if isinstance(el,list): > > >> res.extend(flatten(el)) > > >> else: > > >> res.

Re: Flattening lists

2009-02-05 Thread Stephen Hansen
> Either list creation is somewhat > costly, or "if var is None" is really cheap. "if x is y" is extremely cheap, I believe. Unlike most comparisons which are (relatively) expensive, that one is just comparing simple object address. You can't override "is" so there's a whole series of checks that

Re: Flattening lists

2009-02-05 Thread mk
Brian Allen Vanderburg II wrote: Is list creation really more costly than above? Probably not. I wrote a small test program using a list several levels deep, each list containing 5 sublists at each level and finally just a list of numbers. Flattening 1000 times took about 3.9 seconds for th

Re: Flattening lists

2009-02-05 Thread Brian Allen Vanderburg II
mrk...@gmail.com wrote: Baolong zhen wrote: less list creation. At the cost of doing this at each 'flatten' call: if res is None: res = [] The number of situations of executing above code is the same as the number of list creations (once for each 'flatten' call, obviously). Is list cre

Re: Flattening lists

2009-02-05 Thread mk
Michele Simionato wrote: Looks fine to me. In some situations you may also use hasattr(el, '__iter__') instead of isinstance(el, list) (it depends if you want to flatten generic iterables or only lists). Thanks! Such stuff is what I'm looking for. Regards, mk -- http://mail.python.org/mailma

Re: Flattening lists

2009-02-05 Thread mk
Mark Dickinson wrote: I often find myself needing a 'concat' method that turns a list of lists (or iterable of iterables) into a single list; itertools.chain does this quite nicely. But I don't think I've ever encountered a need for the full recursive version. You're most probably right in th

Re: Flattening lists

2009-02-05 Thread jason-sage
mk wrote: Hello everybody, Any better solution than this? def flatten(x): res = [] for el in x: if isinstance(el,list): res.extend(flatten(el)) else: res.append(el) return res a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]] print flatten(a) It

Re: Flattening lists

2009-02-05 Thread Michele Simionato
On Feb 5, 2:17 pm, mk wrote: > Hello everybody, > > Any better solution than this? > > def flatten(x): >      res = [] >      for el in x: >          if isinstance(el,list): >              res.extend(flatten(el)) >          else: >              res.append(el) >      return res > > a = [1, 2, 3, [4

Re: Flattening lists

2009-02-05 Thread mk
Baolong zhen wrote: less list creation. At the cost of doing this at each 'flatten' call: if res is None: res = [] The number of situations of executing above code is the same as the number of list creations (once for each 'flatten' call, obviously). Is list creation really more costly

Re: Flattening lists

2009-02-05 Thread Mark Dickinson
On Feb 5, 1:17 pm, mk wrote: > Hello everybody, > > Any better solution than this? > > def flatten(x): Just out of interest, how often do people really need such a recursive flatten, as opposed to a single-level version? I often find myself needing a 'concat' method that turns a list of lists (o

Re: Flattening lists

2009-02-05 Thread Baolong zhen
less list creation. On Thu, Feb 5, 2009 at 10:17 PM, mk wrote: > Brian Allen Vanderburg II wrote: > >> def flatten(x): > >> res = [] > >> for el in x: > >> if isinstance(el,list): > >> res.extend(flatten(el)) > >> else: > >> res.append(el) > >>

Re: Flattening lists

2009-02-05 Thread mk
Brian Allen Vanderburg II wrote: def flatten(x): res = [] for el in x: if isinstance(el,list): res.extend(flatten(el)) else: res.append(el) return res I think it may be just a 'little' more efficient to do this: def flatten(x, res=None):

Re: Flattening lists

2009-02-05 Thread Shane Geiger
These functions come from goopy: def flatten1(seq): """ Return a list with the contents of SEQ with sub-lists and tuples "exploded". This is only done one-level deep. """ lst = [] for x in seq: if type(x) is list or type(x) is tuple: for val in x: lst.append(val) else:

Re: Flattening lists

2009-02-05 Thread mk
Brian Allen Vanderburg II wrote: >> def flatten(x): >> res = [] >> for el in x: >> if isinstance(el,list): >> res.extend(flatten(el)) >> else: >> res.append(el) >> return res > > I think it may be just a 'little' more efficient to do this: > > d

Re: Flattening lists

2009-02-05 Thread Brian Allen Vanderburg II
mrk...@gmail.com wrote: Hello everybody, Any better solution than this? def flatten(x): res = [] for el in x: if isinstance(el,list): res.extend(flatten(el)) else: res.append(el) return res a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]] print fl

Flattening lists

2009-02-05 Thread mk
Hello everybody, Any better solution than this? def flatten(x): res = [] for el in x: if isinstance(el,list): res.extend(flatten(el)) else: res.append(el) return res a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]] print flatten(a) [1, 2, 3, 4, 5