【发布时间】:2014-05-15 20:41:22
【问题描述】:
在使用 Python 的 Library Networkx 的函数 write_adjlist (source code) 时遇到以下问题:
输出如下所示:
164021756 15579697
836289488
268525305
527465237 1514162604
460419343
317218275
397533608
37880000
39066509
1146692844
什么时候应该是这样的:
164021756 15579697 836289488 268525305
527465237 1514162604
460419343 317218275
397533608 37880000
39066509 1146692844
我不能真正给你数据,因为它有数百万个节点(这可能是这里的一个因素,虽然我不这么认为)但这基本上是我到达那里的方式:
G = nx.DiGraph()
graph_file = open(filename, 'r')
for line in graph_file.readlines():
try:
x, y =line.replace('\n','').split(',')
except: print "didn't work"; continue;
G.add_edge(x,y)
G.add_edge(y,x)
#This is because it's undirected, but I need the relationships
to be presented on both nodes
nx.write_adjlist(G,outfilename)
graph_file 以 userid1,userid2 的形式显示\n
此代码适用于 2k 节点图和 16k 节点图。
错误可能是由于源代码中的 generate_adjlist 函数,但我不太确定。我也感谢所有有关创建邻接列表的其他方法的帮助和建议。
规格:Ubuntu 14.04 64 位、32GB RAM、SSD、AMD FX(tm)-8350 八核处理器
编辑:这就是 graph_file 的样子:
212127041,218628098
840686875,2278293507
1854227586,2278293507
2266167497,2278293507
2254676097,2278293507
2240955304,2278293507
2226709709,2278293507
1859242609,2278293507
341722764,2278293507
1270686055,2278293507
1049821634,2278293507
1003015644,2278293507
616403983,2278293507
556471190,2278293507
27260086,2278293507
714928003,2278293507
1270696736,2278293507
586671909,2278293507
34507480,2278293507
【问题讨论】:
-
你能显示文件“graph_file”中的一小段数据吗?
-
完成!它只是格式为“用户,目标”
-
您是否在 python 2 解释器中运行为 python 3 制作的 networkx 版本,反之亦然?只是确保。
-
如果你想要无向边,为什么不使用
nx.Graph而不是DiGraph? -
除了@otus answer之外,您可能还希望将节点转换为整数或使用
nx.read_edgelist(filename, delimiter=",", nodetype=int)