当前位置: 首页>>代码示例>>Python>>正文


Python Tree.edges方法代码示例

本文整理汇总了Python中treelib.Tree.edges方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.edges方法的具体用法?Python Tree.edges怎么用?Python Tree.edges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在treelib.Tree的用法示例。


在下文中一共展示了Tree.edges方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load

# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import edges [as 别名]

#.........这里部分代码省略.........
	graph = None
	nodes = []

	#TEMP: LOAD FILE FROM PATH#
	file = open(path,"r",0)
	#TEMP#

	gtype = file.next() 
	#print isInt(gtype) #ASSERT LINE 0 IS AN INTEGER
	gtype = int(gtype)
	#print gtype

	if (gtype == 0):
		graph = Tree()
		nodes = []
		print 'GRAPH TYPE: TREE'

	if (gtype == 1):
		graph = digraph()		
		print 'GRAPH TYPE: EWD'

	#print type(graph)
	for linenum,line in enumerate(file):
		if (linenum == 0): 
			parts = line.split()
			#print parts[0]

			nodeAttrs = parts[1].split(';')
			numNodeAttrs = nodeAttrs.pop(0)
			#print numNodeAttrs
			#print isInt(numNodeAttrs)

			#print numNodeAttrs, nodeAttrs
			continue

		if (linenum == 1):
			for node in line.split():
				tokens = node.split(';')
				index = tokens.pop(0)
				#print index #in final, assert numNodes == index
				#print isInt(index)
				if (isInt(index)):
					index = int(index)
					if (gtype == 0):
						nodes.append(Node(identifier=index,data=tokens))
						if (index == 0):
							#print "ZERO"
							#n = nodes[0]
							#print type(n)
							graph.add_node(nodes[0])
							#graph.show()
							#print graph.get_node(0)
					if (gtype == 1):
						graph.add_node(index, attrs=tokens)
					#print tokens
					continue

		if (linenum == 2):
			if (gtype == 0):
				#print isInt(line) #ASSERT LINE IS A SINGLE INT
				numEdges = int(line)
			if (gtype == 1):
				parts = line.split()
				#print parts[0]

				lineAttrs = parts[1].split(';')
				numLineAttrs = lineAttrs.pop(0)
				#print numLineAttrs
				#print isInt(numLineAttrs)

				#print numLineAttrs, lineAttrs

				continue

		if (linenum > 2):
			#CHECK THAT PARTS ARE INT
			parts = line.split()
			tail = int(parts[0])
			head = int(parts[1])

			if (gtype == 0):
				graph.add_node(nodes[head],tail)

			if (gtype == 1):
				attributes = parts[2].split(';')
				weight = attributes.pop(0)
				#check head and tail are integers

				#check number attributes

				#print (tail,head), weight, attributes
				graph.add_edge((tail,head),weight,attrs=attributes)

	if (gtype == 0):
		graph.show()
	if (gtype == 1):
		for node in graph.nodes():
			print node, graph.node_attr[node]
		for edge in graph.edges():
			print edge, graph.edge_weight(edge),  graph.edge_attr[edge]
开发者ID:KAKSteam,项目名称:2631GP,代码行数:104,代码来源:loader.py


注:本文中的treelib.Tree.edges方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。