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


Groups > comp.lang.python > #70039

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '[1,': 0.09; 'function,': 0.09; 'modifies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'def': 0.12; 'in- place': 0.16; 'logiciel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:make': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; '---': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'est': 0.30; '>>>>': 0.31; 'but': 0.35; 'to:addr:python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'received:62': 0.63; 'protection': 0.63; 'skip:n 10': 0.64; 'virus': 0.65; 'antivirus': 0.68; 'courrier': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Boris Borcic <bborcic@gmail.com>
Subject Re: how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?
Date Thu, 10 Apr 2014 16:14:55 +0200
References <CAOmh462zZbe29dPL1nV7YokJObwuHXu0_K5PxD1gUr_=RB73mA@mail.gmail.com> <mailman.9115.1397107547.18130.python-list@python.org> <f9590411-eab6-464f-9a8f-a5018a0f377d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host cust.static.62-202-12-176.swisscomdata.ch
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0 SeaMonkey/2.25
In-Reply-To <f9590411-eab6-464f-9a8f-a5018a0f377d@googlegroups.com>
X-Antivirus avast! (VPS 140410-0, 10.04.2014), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9137.1397139309.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1397139309 news.xs4all.nl 2971 [2001:888:2000:d::a6]:41204
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70039

Show key headers only | View raw


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 | NextPrevious in thread | Find similar | Unroll thread


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