Add tags

Add tags to the following comment

素直に再帰関数で実装しました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# encoding: utf-8
list = [ "abc\\def", "abc\\def\\gh", "abc\\def\\ij", "abc\\jk\\lm", "de" ]

def pathtree(lst):
  def maketree(l):
    s = [x for x in set([v[0] for v in l])]
    s.sort()
    return [[v, maketree([x[1:] for x in l if x[0]==v and len(x)>1])] for v in s]
  return ["ROOT", maketree([s.split("\\") for s in list])]

def printtree(tree):
  def printchild(tree, nest):
    for c in tree:
      if c==[]: continue
      print " "*nest*2 + "┗" + c[0]
      printchild(c[1], nest+1)
  print tree[0]
  printchild(tree[1], 0)

printtree(pathtree(list))

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...