Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70039
| From | Boris Borcic <bborcic@gmail.com> |
|---|---|
| Subject | Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ? |
| Date | 2014-04-10 16:14 +0200 |
| References | <CAOmh462zZbe29dPL1nV7YokJObwuHXu0_K5PxD1gUr_=RB73mA@mail.gmail.com> <mailman.9115.1397107547.18130.python-list@python.org> <f9590411-eab6-464f-9a8f-a5018a0f377d@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9137.1397139309.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web