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


Groups > comp.lang.python > #31872

Re: Recursive Generator Error?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!news2.arglkargh.de!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <zhushenli@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'skip:[ 20': 0.03; 'elif': 0.04; 'output': 0.04; 'say,': 0.05; 'function,': 0.07; 'subject:Error': 0.07; 'python': 0.09; '22,': 0.09; 'output,': 0.09; 'structure,': 0.09; 'terry': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; ';-)': 0.11; 'clear.': 0.16; 'discarded.': 0.16; 'expect,': 0.16; 'grouped': 0.16; 'hint:': 0.16; 'reedy': 0.16; 'specified.': 0.16; 'tables,': 0.16; 'threw': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'jan': 0.18; 'written': 0.20; 'cc:2**0': 0.23; 'elements': 0.23; 'monday,': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'hash': 0.29; "skip:' 10": 0.30; 'basic': 0.30; 'function': 0.30; 'lists': 0.31; 'code': 0.31; 'print': 0.32; 'right?': 0.33; 'traceback': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'entered': 0.34; 'pm,': 0.35; 'table': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; "didn't": 0.36; 'anything': 0.36; 'thank': 0.36; 'october': 0.37; 'one,': 0.37; 'does': 0.37; 'item': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'called': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'visit': 0.64; 'become': 0.65; 'harder': 0.65; 'wish.': 0.84; 'works!': 0.84
Newsgroups comp.lang.python
Date Sun, 21 Oct 2012 17:40:41 -0700 (PDT)
In-Reply-To <mailman.2608.1350863992.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=114.91.107.108; posting-account=0SDsjAoAAACST9_yxvTp0Bl9-m-EasZx
References <d1dbc8a7-1ec6-474c-96d7-b95a066125dd@googlegroups.com> <mailman.2608.1350863992.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 114.91.107.108
MIME-Version 1.0
Subject Re: Recursive Generator Error?
From David <zhushenli@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2614.1350866449.27098.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350866449 news.xs4all.nl 6851 [2001:888:2000:d::a6]:43047
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31872

Show key headers only | View raw


On Monday, October 22, 2012 7:59:53 AM UTC+8, Terry Reedy wrote:
> On 10/21/2012 7:29 PM, David wrote:
> 
> > I have a tree-like data structure, the basic elements are hash tables,
> 
> > and they are grouped into lists, like [[{'a':1},[{'b':2}]]].
> 
> > And I want to flat the lists and visit hash table one by one, like {'a':1}, {'b':2}.
> 
> > But my program didn't work as I wish. When it entered the 2nd
> 
> > flat_yield, it threw a GeneratorExit. Is there anything wrong?
> 
> 
> 
> 1. The Python version is not specified.
> 
> 2. You used 2.x; in 3.3 the code does exactly what I would expect, which 
> 
> is to say, nothing. No output, no error, no traceback ;-)
> 
> 3. The traceback is missing from this post.
> 
> 
> 
> > #- - - - - - - - - -
> 
> > def flat_yield(tbl_list):
> 
> >      for t in tbl_list:
> 
> >          if type(t) == type({}):
> 
> >              yield t
> 
> >          elif type(t) == type([]):
> 
> >              flat_yield(t)
> 
> 
> 
> 4. Think harder about what that expression does.
> 
> 
> 
> > a = [[{'a':1},[{'b':2}]]]
> 
> > for i in flat_yield(a):
> 
> >      print i
> 
> 
> 
> Hint: it calls flat_yield, which returns a generator, which is then 
> 
> discarded. You might have well written 'pass'.
> 
> 
> 
> Solution: use the recursively called generator and recursively yield 
> 
> what it yields. Replace 'flat_yield(t)' with
> 
> 
> 
>              for item in flat_yield(t):
> 
>                  yield item
> 
> 
> 
> and the output is what you want.
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

Hi Terry, thank you! I use Python 2.7, and your solution works!

I have thought harder, still not very clear. 

If I have one "yield" in function, the function will become generator, and it can only be called in the form like "for item in function()" or "function.next()", and call the function directly will raise error, is it right?

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


Thread

Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 16:29 -0700
  Re: Recursive Generator Error? Terry Reedy <tjreedy@udel.edu> - 2012-10-21 19:59 -0400
    Re: Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 17:40 -0700
    Re: Recursive Generator Error? David <zhushenli@gmail.com> - 2012-10-21 17:40 -0700
      Re: Recursive Generator Error? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-22 02:36 +0000

csiph-web