Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70012 > unrolled thread
| Started by | balaji marisetti <balajimarisetti@gmail.com> |
|---|---|
| First post | 2014-04-10 10:55 +0530 |
| Last post | 2014-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.
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
| From | balaji marisetti <balajimarisetti@gmail.com> |
|---|---|
| Date | 2014-04-10 10:55 +0530 |
| Subject | Re: 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]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-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]
| From | Boris Borcic <bborcic@gmail.com> |
|---|---|
| Date | 2014-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