Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>\r</code> is probably not a portable solution, the way it is rendered will depend on whatever text editor or terminal you're using. On older Mac systems, <code>'\r'</code> is was used as the end of line character(On windows it is <code>'\r\n'</code> and on linux and OSX it is <code>'\n'</code>.</p> <p>You could simply do something like this:</p> <pre><code>def print_lines_at_same_position(*lines): prev_len = 0 for line in lines: print " "*prev_len + line prev_len += len(line) </code></pre> <p>Usage example:</p> <pre><code>&gt;&gt;&gt; print_lines_at_same_position("hello", "world", "this is a test") hello world this is a test &gt;&gt;&gt; </code></pre> <p>This will only work if whatever you're outputting to has a font with a fixed character length though. I can't think of anything that will work otherwise</p> <p><strong>Edit to fit changed question</strong></p> <p>Okay, so that's an entirely different question. I don't think there's any way to do it with it starting at <em>exactly</em> the position where the last line left off unless <code>self.key</code> has a predictable length. But you can get something pretty close with this:</p> <pre><code>class node: def __init__(self,key,children): self.key = key self.children = children self.depth = 0 def set_depth(self, depth): self.depth = depth for child in self.children: child.set_depth(depth+1) def __str__(self): indent = " "*4*self.depth children_str = "\n".join(map(str, self.children)) if children_str: children_str = "\n" + children_str return indent + "Node: %s%s" % (self.key, children_str) </code></pre> <p>Then just set the depth of the root node to 0 and do that again every time you change the structure of the tree. There are more efficient ways if you know exactly how you're changing the tree, you can probably figure those out yourself :)</p> <p>Usage example:</p> <pre><code>&gt;&gt;&gt; a = node("leaf", []) &gt;&gt;&gt; b = node("another leaf", []) &gt;&gt;&gt; c = node("internal", [a,b]) &gt;&gt;&gt; d = node("root", [c]) &gt;&gt;&gt; d.set_depth(0) &gt;&gt;&gt; print d Node: root Node: internal Node: leaf Node: another leaf &gt;&gt;&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload