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


Groups > comp.lang.python > #54765

Re: Understanding how is a function evaluated using recursion

Path csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.04; 'nested': 0.07; 'returned.': 0.07; 'append': 0.09; 'item.': 0.09; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; 'itself.': 0.14; 'first:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'list)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'subject:recursion': 0.16; 'sublist': 0.16; 'which,': 0.16; 'wrote:': 0.18; 'items.': 0.19; 'skip:f 30': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'simpler': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'prints': 0.31; 'lists': 0.32; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'doing': 0.36; 'hi,': 0.36; 'example,': 0.37; 'list.': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'flat': 0.60; 'tell': 0.60; '(that': 0.65; 'here': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'does!': 0.84; 'reply- to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=MqNrtQqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=Oj17QQNopW4A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=iVdR1OCPcRcA:10 a=6MS-rkTbuIvzjTTkGdYA:9 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Thu, 26 Sep 2013 01:07:01 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: Understanding how is a function evaluated using recursion
References <231e5958-97c9-489c-9cfa-0f4451f6520c@googlegroups.com>
In-Reply-To <231e5958-97c9-489c-9cfa-0f4451f6520c@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
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.327.1380154018.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380154018 news.xs4all.nl 15872 [2001:888:2000:d::a6]:40594
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54765

Show key headers only | View raw


On 26/09/2013 00:24, Arturo B wrote:
> Hi, I'm doing Python exercises and I need to write a function to flat nested lists
> as this one:
>
> [[1,2,3],4,5,[6,[7,8]]]
>
> To the result:
>
> [1,2,3,4,5,6,7,8]
>
> So I searched for example code and I found this one that uses recursion (that I don't understand):
>
> def flatten(l):
>      ret = []
>      for i in l:
>          if isinstance(i, list) or isinstance(i, tuple):
>              ret.extend(flatten(i)) #How is flatten(i) evaluated?
>          else:
>              ret.append(i)
>      return ret
>
> So I know what recursion is, but I don't know how is
>
>                         flatten(i)
>
> evaluated, what value does it returns?
>
Try a simpler version first:

def flatten(l):
     ret = []
     for i in l:
         if isinstance(i, list) or isinstance(i, tuple):
             # Append the contents of the item.
             ret.extend(i)
         else:
             # Append the item itself.
             ret.append(i)
     return ret

In this example, flatten([[1,2,3],4,5,[6,[7,8]]]) returns [1,2,3,4,5,6,
[7,8]].

The problem here is that a sublist can itself contain a list.

It would be nice if there were a function which, when given [6,[7,8]],
would return [6,7,8] so that you could append those items.

But that's exactly what flatten does!

Try adding prints to tell you what was passed in and what is returned.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Understanding how is a function evaluated using recursion Arturo B <a7xrturodev@gmail.com> - 2013-09-25 16:24 -0700
  Re: Understanding how is a function evaluated using recursion Josh English <Joshua.R.English@gmail.com> - 2013-09-25 16:59 -0700
  Re: Understanding how is a function evaluated using recursion MRAB <python@mrabarnett.plus.com> - 2013-09-26 01:07 +0100
  Re: Understanding how is a function evaluated using recursion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-26 00:10 +0000
  Re: Understanding how is a function evaluated using recursion Dave Angel <davea@davea.name> - 2013-09-26 00:26 +0000
  Re: Understanding how is a function evaluated using recursion Terry Reedy <tjreedy@udel.edu> - 2013-09-25 21:12 -0400
  Re: Understanding how is a function evaluated using recursion rusi <rustompmody@gmail.com> - 2013-09-25 21:04 -0700
  Re: Understanding how is a function evaluated using recursion Neil Cerutti <neilc@norwich.edu> - 2013-09-26 14:18 +0000
    Re: Understanding how is a function evaluated using recursion Neil Cerutti <neilc@norwich.edu> - 2013-09-26 14:23 +0000
      Re: Understanding how is a function evaluated using recursion Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-28 13:24 -0700
  Re: Understanding how is a function evaluated using recursion rusi <rustompmody@gmail.com> - 2013-09-29 20:41 -0700

csiph-web