Category tree of some sort ...

logistix logistix at zworg.com
Fri Dec 6 22:07:35 EST 2002


"Thomas Weholt" <2002 at weholt.org> wrote in message news:<iRaI9.6141$hV6.105539 at news2.e.nsc.no>...
> I got a bunch of content categories in form of a list of lists :
> 
> categories = [
>         ['computers','programming','python'],
>         ['computers','programming','c'],
>         ['computers','technical','os']
> ]
> 
> I need to put this into a tree-structure, a dictionary like this :
> 
> category_tree = {
>     'computers': ([], {
>         'programming': ([], {
>             'python': ( [], {}),
>             'c': ( [], {}),
>         },
>     'technical': ([], {
>         'os': ([], {}),
>         }
>     }
> 
> Each level holds a list of ids, pointing to resources on a local disk, the
> dict-part of each tuple is the base for a another, deeper level in the
> tree-structure.
> 
> If anybody has any other idea on how to effectivly storing information about
> files where the folders are names of categories and each file is assigned a
> uniq id for look-up, please let me know.
> 
> Thanks,
> Thomas

You could also make a node class.  I think this is easier to deal with
once your tree gets more complex (although it uses more memory on the
heap and stack).

>>> class node:
... 	def __init__(self, name, parent=None):
... 		self.name, self.parent = name, parent
... 		self.children = []
... 	def addChild(self, childName):
... 		child = node(childName, self)
... 		self.children.append(child)
... 		return child
... 	
>>> root = node("computer")
>>> programming = root.addChild("programming")
>>> programming.addChild("python")

and then from there, add more methods to the class to climb through
the tree and process as you see fit.

or, you could just crawl the filesystem directly in python and use the
fs as your database.



More information about the Python-list mailing list