数组关联 Python
Array Associations Python
我有一个模仿树状图的 CSV 文件,如下所示:
A,B #A is associated to B
A,C #A is associated to C
B,D #B is associated to D
B,E #B is associated to E
C,F #C is associated to F
C,G #C is associated to G
A 是根(树的顶部)B & C 是分支,D、E、F、G 是叶子(分支的子级)
我想知道是否有办法将它与它的关联放在一个数组中?
使用networkx to make a digraph and matplotlib绘制有向图的图像:
import networkx as nx
import matplotlib.pyplot as plt
text =\
"""A,B
A,C
B,D
B,E
C,F
C,G"""
graph = nx.DiGraph()
for i in text.split('\n'):
graph.add_edge(i[0], i[2])
nx.draw(graph, with_labels=True)
plt.show()
注意这一行:graph.add_edge(i[0], i[2])
如果此图中不存在该节点,则会自动创建它。
剧情:
保存信息的自然结构是 字典,其中每个条目的键是一个节点名称(例如,C),出现在您的 csv 文件和该 dict 条目的值是一个数组,该数组最终包含作为该键的直接子节点的所有节点的名称(例如,[F, G])。
因此,当您处理 csv 文件的每一行(例如 B、D)时,检查第一个元素(例如 B)是否已经是字典中的键。如果不是,则使用 key=B 和 value=[D] 向 dict 添加一个条目。如果它已经存在,则将第二个元素附加到该键的数组值。例如,当您到达数据行 B、E 时,您会找到 B 的条目 (B => [D]) 并将 E 附加到它的值上,从而产生 [D, E].
完成后,如果节点的名称从未出现在任何条目的数组值中,则该节点就是树的根。如果不止一个节点有这个 属性,那么这些节点中的每一个都是树木森林中一棵树的根。
假设文件 my.csv
包含您提供的示例输入,此代码将图形结构记录在两个简单的 Python dict
对象中:
parent = {}
children = {}
with open( 'my.csv', 'rt' ) as fh:
for line in fh:
# strip the comments and line endings
if '#' in line: line = line[ :line.index( '#' ) ]
line = line.strip()
if line:
# record the association
node, subnode = line.split( ',', 1 )
parent[ subnode ] = node
children.setdefault( node, [] ).append( subnode )
然后语法 children['A']
允许您查找属于节点 'A'
的子节点列表,而语法 parent['B']
则让您走另一条路,查找节点 'B'
的父节点。或者你可以 pretty-print 整件事:
for node, subnodes in sorted( children.items() ):
print( '%r : %r' % ( node, subnodes ) )
输出:
'A' : ['B', 'C']
'B' : ['D', 'E']
'C' : ['F', 'G']
我有一个模仿树状图的 CSV 文件,如下所示:
A,B #A is associated to B
A,C #A is associated to C
B,D #B is associated to D
B,E #B is associated to E
C,F #C is associated to F
C,G #C is associated to G
A 是根(树的顶部)B & C 是分支,D、E、F、G 是叶子(分支的子级)
我想知道是否有办法将它与它的关联放在一个数组中?
使用networkx to make a digraph and matplotlib绘制有向图的图像:
import networkx as nx
import matplotlib.pyplot as plt
text =\
"""A,B
A,C
B,D
B,E
C,F
C,G"""
graph = nx.DiGraph()
for i in text.split('\n'):
graph.add_edge(i[0], i[2])
nx.draw(graph, with_labels=True)
plt.show()
注意这一行:graph.add_edge(i[0], i[2])
如果此图中不存在该节点,则会自动创建它。
剧情:
保存信息的自然结构是 字典,其中每个条目的键是一个节点名称(例如,C),出现在您的 csv 文件和该 dict 条目的值是一个数组,该数组最终包含作为该键的直接子节点的所有节点的名称(例如,[F, G])。
因此,当您处理 csv 文件的每一行(例如 B、D)时,检查第一个元素(例如 B)是否已经是字典中的键。如果不是,则使用 key=B 和 value=[D] 向 dict 添加一个条目。如果它已经存在,则将第二个元素附加到该键的数组值。例如,当您到达数据行 B、E 时,您会找到 B 的条目 (B => [D]) 并将 E 附加到它的值上,从而产生 [D, E].
完成后,如果节点的名称从未出现在任何条目的数组值中,则该节点就是树的根。如果不止一个节点有这个 属性,那么这些节点中的每一个都是树木森林中一棵树的根。
假设文件 my.csv
包含您提供的示例输入,此代码将图形结构记录在两个简单的 Python dict
对象中:
parent = {}
children = {}
with open( 'my.csv', 'rt' ) as fh:
for line in fh:
# strip the comments and line endings
if '#' in line: line = line[ :line.index( '#' ) ]
line = line.strip()
if line:
# record the association
node, subnode = line.split( ',', 1 )
parent[ subnode ] = node
children.setdefault( node, [] ).append( subnode )
然后语法 children['A']
允许您查找属于节点 'A'
的子节点列表,而语法 parent['B']
则让您走另一条路,查找节点 'B'
的父节点。或者你可以 pretty-print 整件事:
for node, subnodes in sorted( children.items() ):
print( '%r : %r' % ( node, subnodes ) )
输出:
'A' : ['B', 'C']
'B' : ['D', 'E']
'C' : ['F', 'G']