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


Groups > comp.lang.python > #70012 > unrolled thread

Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?

Started bybalaji marisetti <balajimarisetti@gmail.com>
First post2014-04-10 10:55 +0530
Last post2014-04-10 16:14 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ? balaji marisetti <balajimarisetti@gmail.com> - 2014-04-10 10:55 +0530
    Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ? Rustom Mody <rustompmody@gmail.com> - 2014-04-09 22:38 -0700
      Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ? Boris Borcic <bborcic@gmail.com> - 2014-04-10 16:14 +0200

#70012 — Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?

Frombalaji marisetti <balajimarisetti@gmail.com>
Date2014-04-10 10:55 +0530
SubjectRe: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?
Message-ID<mailman.9115.1397107547.18130.python-list@python.org>
There was long thread discussing flattening of a list on this list :).
See the link below.

https://mail.python.org/pipermail/python-list/2014-March/669256.html

On 10 April 2014 10:44, length power <elearn2014@gmail.com> wrote:
>>>> x=["a","b",["c","d"],"e"]
>>>> y=x[2]
>>>> y
> ['c', 'd']
>>>> x.insert(2,y[0])
>>>> x
> ['a', 'b', 'c', ['c', 'd'], 'e']
>>>> x.insert(3,y[1])
>>>> x
> ['a', 'b', 'c', 'd', ['c', 'd'], 'e']
>>>> del x[4]
>>>> x
> ['a', 'b', 'c', 'd', 'e']
>>>>
> maybe there is a more smart way to do.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
:-)balaji

[toc] | [next] | [standalone]


#70014

FromRustom Mody <rustompmody@gmail.com>
Date2014-04-09 22:38 -0700
Message-ID<f9590411-eab6-464f-9a8f-a5018a0f377d@googlegroups.com>
In reply to#70012
On Thursday, April 10, 2014 10:55:10 AM UTC+5:30, balaji marisetti wrote:
> There was long thread discussing flattening of a list on this list :).
> See the link below.

I dont think that thread is relevant to this question:
1. That (started with) a strange/cute way of using names
2. It does not work for heterogenous lists

# One level flatten version
# like the earlier thread without strange naming
# needed by the recursive one below
>>> def fl1(l): return [y for x in l for y in x]


# recursive flatten
>>> def fr(l):
...   if not isinstance(l,list): return [l]
...   return fl1([fr(x) for x in l])


You can replace the list-comps by generator-comps 

[toc] | [prev] | [next] | [standalone]


#70039

FromBoris Borcic <bborcic@gmail.com>
Date2014-04-10 16:14 +0200
Message-ID<mailman.9137.1397139309.18130.python-list@python.org>
In reply to#70014
Rustom Mody wrote:
>>>> def fl1(l): return [y for x in l for y in x]
>
>
> # recursive flatten
>>>> def fr(l):
> ...   if not isinstance(l,list): return [l]
> ...   return fl1([fr(x) for x in l])

For a short non-recursive procedure - not a function, modifies L in-place but none of its sublists.

 >>> def flatten(L) :
....    for k,_ in enumerate(L) :
....        while isinstance(L[k],list) :
....            L[k:k+1]=L[k]

flattens L to any depth, eg

 >>> L=[1,[2,[3,4]],5,6,[[7],8]]
 >>> flatten(L)
 >>> L
[1, 2, 3, 4, 5, 6, 7, 8]


---
Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.
http://www.avast.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web