1

Alright, i am new to python, and simple tab creation in this language somewhat bewilders me. I am trying to get a tabbed output like such:

:0.1
  A:0.1
  :0.9
    H:2.2
    I:3.0
    B:0.2
  :0.5
    C:0.3
    D:0.4

It is the name of a node, a colon,':', and its corresponding distance, the nodes with no name are designated by 'None' and that is because they represent only a distance to another variable. This is a tree design, so the tabs and indentations can vary. I gather this information from a

self.name

representing the name variable, and a

self.distance

representing the distance

An unindented output of the information looks like the following:

A : 0.1
H : 2.2
I : 3.0
B : 0.2
None : 0.9
C : 0.3
D : 0.4
None : 0.5
None : 0.1

There are supposed to be 3 levels of indentations,

the root, ':0.1',

its 3 children of 'A:0.1'; ':0.9'; ':0.5',

and ':0.9' and ':0.5's children, H,I,B,C and D

I apologize if this is not enough information, i am just unsure how to create a basic tabbed output like the one i've shown above.

Thanks!

EDIT: received my answer Thank You!

6
  • "\t" will make a tab ... Commented Jul 9, 2012 at 20:32
  • 3
    Can you post the code that you used to generate this? Commented Jul 9, 2012 at 20:32
  • 1
    If you're using recursion to traverse the data structure, then you can easily pass a depth argument in your function and prepend something like "\t" * depth to your output. Commented Jul 9, 2012 at 20:35
  • ahh, and the depth varies accordingly with my code, thank you, i did not know of this ability Commented Jul 9, 2012 at 20:39
  • @Sean, you should mention the person who gave you the answer and ask them to post the comment as an answer so you can mark it as accepted! Commented Jul 9, 2012 at 20:43

1 Answer 1

4

Let's say you have a tree-like data structure. I'm going to use a nested dict in this example for simplicity:

data = {
    "A": {"value": 0.2, "children": {
            "D": {"value": 0.3, "children": {}},
            "E": {"value": 0.4, "children": {
                    "H": {"value": 0.5, "children": {}},
                    "I": {"value": 0.6, "children": {}}
                }
            }
        }
    },
    "B": {"value": 0.7, "children": {
            "C": {"value": 0.8, "children": {}},
            "D": {"value": 0.9, "children": {}}
        }
    }
}

You could traverse and print it using the following recursive function:

from operator import itemgetter

def display(tree, depth=0):
    prepend = "\t" * depth
    for key, val in sorted(tree.items()):
        print "{0}{1}: {2}".format(prepend, key, val['value'])
        if val['children']:
            display(val['children'], depth + 1)

Which displays the following output:

>>> display(data)
A: 0.2
    D: 0.3
    E: 0.4
        H: 0.5
        I: 0.6
B: 0.7
    C: 0.8
    D: 0.9
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

When possible -- as it is here -- I prefer to iterate over the sorted keys (or key, val pairs). Not because dictionaries are somehow sorted, just because it makes the output reproducible and therefore easier to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.