开发者

building a tree from a csv file using python

开发者 https://www.devze.com 2023-02-18 15:09 出处:网络
My csv file has the following format Col1Col2 ab bc cd de xb yc zc mx hb ib I will create a dictionary to hold this data like this

My csv file has the following format

Col1        Col2
a           b
b           c
c           d
d           e
x           b
y           c
z           c
m           x
h           b
i           b

I will create a dictionary to hold this data like this

{ b:[a,x,h,i] , c:[b,y,z], d:[c], e:[d], x:[m] } 

From this dictionary I want to be able to build a hierarchy. For example: when I walk through the dictionary for 'a' I should be able to display

 a -> b -> c -> d -> e

Similarly for 'y'

 y -> c -> d -> e

I can开发者_开发技巧 think of this as a tree structure and imagine this to be a depth first traversal, but I am unsure about how to achieve this with a dictionary in python. This would not be a decision tree or a binary tree etc.


You can use Python-Graph.

pairs = read_from_csv(...)

from pygraph.classes.digraph import digraph 
gr = digraph()
gr.add_nodes(set([x for (x,y) in pairs]+[y for (x,y) in pairs]))

for pair in pairs:
    gr.add_edge(pair)

#and now you can do something with the graph...

from pygraph.algorithms.searching import depth_first_search

print ' -> '.join(depth_first_search(gr, root='a')[1])
print ' -> '.join(depth_first_search(gr, root='y')[1])


Pseudocode:

filedict = {}
for row in file:
  try:
    filedict[row.col2].append(row.col1)
  except:
    filedict[row.col2] = [row.col1]
invdict = dict((v,k) for k, v in filedict.iteritems())
def parse(start):
  if start not in invdict:
    return []
  next = invdict[start]
  return [next] + parse(next)


Here is a solution that just uses dictionaries:

from itertools import chain

def walk(d, k):
    print k,
    while k in d:
        k = d[k]
        print '->', k,

data = {'b': ['a','x','h','i'], 'c': ['b','y','z'], 'd': ['c'], 'e': ['d'], 'x': ['m']}
hierarchy = dict(chain(*([(c, p) for c in l] for p, l in data.iteritems())))
# {'a':'b', 'c':'d', 'b':'c', 'd':'e', 'i':'b', 'h':'b', 'm':'x', 'y':'c', 'x':'b', 'z':'c'}

walk(hierarchy, 'a') # prints 'a -> b -> c -> d -> e'
walk(hierarchy, 'y') # prints 'y -> c -> d -> e'
0

精彩评论

暂无评论...
验证码 换一张
取 消