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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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.
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).
>
>
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
> 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
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
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
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
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:
>>
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
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.
> 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
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
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
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
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
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
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
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
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
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)
> >>
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):
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:
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
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
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
44 matches
Mail list logo