Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #84943
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!81.171.88.16.MISMATCH!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(even': 0.05; 'elif': 0.05; 'pop': 0.05; 'root': 0.05; 'tree': 0.05; 'element': 0.07; 'nested': 0.07; 'returned.': 0.07; 'variables': 0.07; 'append': 0.09; 'emulate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stack,': 0.09; 'subject:parsing': 0.09; 'def': 0.12; '1):': 0.16; '[none]': 0.16; 'corresponds': 0.16; 'indexerror:': 0.16; 'innermost': 0.16; 'nodes': 0.16; 'notation': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simplified': 0.16; 'stack)': 0.16; 'thinking,': 0.16; 'valueerror': 0.16; 'index': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'stack': 0.19; 'solution.': 0.20; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'necessary.': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'bigger': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'depth': 0.31; 'node': 0.31; 'produces': 0.31; 'another': 0.32; 'quite': 0.32; 'level.': 0.33; 'actual': 0.34; 'subject:from': 0.34; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'version': 0.36; 'next': 0.36; 'two': 0.37; 'list': 0.37; 'level': 0.37; 'to:addr:python-list': 0.38; 'previous': 0.38; 'structure': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'remove': 0.60; 'removing': 0.60; 'new': 0.61; 'complete': 0.62; 'skip:n 10': 0.64; 'more': 0.64; 'levels': 0.65; 'here': 0.66; 'therefore': 0.72 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: parsing tree from excel sheet |
| Date | Sat, 31 Jan 2015 10:07:55 +0100 |
| Organization | None |
| References | <cirqviF15qtU1@mid.individual.net> <mailman.18217.1422454096.18130.python-list@python.org> <cj1kt9Fi3e0U1@mid.individual.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd93d4.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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.18334.1422695619.18130.python-list@python.org> (permalink) |
| Lines | 137 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1422695619 news.xs4all.nl 2928 [2001:888:2000:d::a6]:43334 |
| X-Complaints-To | abuse@xs4all.nl |
| X-Received-Body-CRC | 3506569236 |
| X-Received-Bytes | 6973 |
| Xref | csiph.com comp.lang.python:84943 |
Show key headers only | View raw
alb wrote:
> >
> > def read_tree(rows, levelnames):
> > root = Node("#ROOT", "#ROOT")
> > old_level = 0
> > stack = [root]
> > for i, row in enumerate(rows, 1):
>
> I'm not quite sure I understand what is the stack for. As of now is a
> list whose only element is root.
The stack is used to emulate the function call stack in a recursive
solution. Every nested call produces a new set of local variables and the
innermost call corresponds to the top of the stack or the end of the `stack`
list in my code.
Given a tree
A
A1
A11
A12
A2
or in another notation
0 A
1 A1
2 A11
2 A12
1 A2
we start with a stack (with the node's children in parens)
[A()]
then look at A1 and see that the level is one deeper than that of A and
append it to A, the current TOS (top of stack)
[A(A1)]
Next is A11 which is two levels deeper than A, so we take the last child of
A and put it on the stack,
[A(A1), A1()]
then add A11 as a child to the new TOS
[A(A1), A1(A11)]
Next is A12 which is one level deeper than A1, so we add it to the current
TOS
[A(A1), A1(A11, A12)]
Next is A2 which is one level higher than A1, so we keep removing nodes from
the stack until the current TOS is one level higher than A2. Here we only
have to remove A1 from the stack to get
[A(A1)]
and after adding A2 to the TOS
[A(A1, A2)]
I have ommitted the children of the children so far, but the actual
structure is now
[A(A1(A11(), A12()), A2())]
Therefore I just need to return A which contains the complete layout of the
tree.
> > new_level = column_index(row)
> > node = Node(row[new_level], levelnames[new_level])
>
> here you are getting the node based on the current row, with its level.
>
> > if new_level == old_level:
> > stack[-1].append(node)
>
> I'm not sure I understand here. Why the end of the list and not the
> beginning?
>
> > elif new_level > old_level:
> > if new_level - old_level != 1:
> > raise ValueError
>
> here you avoid having a node which is distant more than one level from
> its parent.
>
> > stack.append(stack[-1].children[-1])
>
> here I get a crash: IndexError: list index out of range!
>
> > stack[-1].append(node)
> > old_level = new_level
> > else:
> > while new_level < old_level:
> > stack.pop(-1)
> > old_level -= 1
> > stack[-1].append(node)
>
> Why do I need to pop something from the stack??? Here you are saying
> that if current row has a depth (new_level) that is smaller than the
> previous one (old_level) I decrement by one the old_level (even if I may
> have a bigger jump) and pop something from the stack...???
>
> > return root
>
> once filled, the tree is returned. I thought the tree would have been
> the stack, but instead is root...nice surprise.
> Why do I need to pop something from the stack???
That got me thinking, and I found that my code is indeed much more complex
than necessary. Sorry for that ;)
As a compensation here is the simplified non-popping version of read_tree():
def read_tree(rows, levelnames):
root = Node("#ROOT", "#ROOT")
parents = [root] + [None] * len(levelnames)
for row in rows:
level = column_index(row)
node = Node(row[level], levelnames[level])
parents[level].append(node)
parents[level+1] = node
return root
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-28 10:12 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-28 15:08 +0100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-28 14:27 +0000
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:02 +0000
Re: parsing tree from excel sheet MRAB <python@mrabarnett.plus.com> - 2015-01-29 21:16 +0000
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:32 +0000
Re: parsing tree from excel sheet Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-29 21:59 +0000
Re: parsing tree from excel sheet Chris Kaynor <ckaynor@zindagigames.com> - 2015-01-29 14:30 -0800
Re: parsing tree from excel sheet Chris Angelico <rosuav@gmail.com> - 2015-01-30 10:46 +1100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-30 15:05 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-30 18:11 +0100
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-31 22:45 +0000
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-02-01 11:11 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-30 18:24 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-31 10:07 +0100
Re: parsing tree from excel sheet Peter Otten <__peter__@web.de> - 2015-01-31 10:07 +0100
Re: parsing tree from excel sheet Tim Chase <python.list@tim.thechases.com> - 2015-01-28 08:13 -0600
Re: parsing tree from excel sheet al.basili@gmail.com (alb) - 2015-01-29 21:22 +0000
csiph-web