我正在尝试从具有以下结构的.txt文件读取图形:
115564928125997351000, ah.creativecodeapps.tiempo,1
117923818995099650007, air.com.agg.popcornmakermarket,-1
104000841215686444437, air.com.zahdoo.cadie,1
.
.
.
我一直在使用以下命令:
g=nx.read_weighted_edgelist('siatoy.txt', delimiter=',',nodetype=str,encoding='utf-8')
但是当我做
g.edges(data=True)
时,我得到了:[('106784557494786869271', ' com.map2app.U5635321228165120A5661458385862656'),
('106784557494786869271',' com.jb.gokeyboard.theme.mzdevelopment.americankeyboard'),
('106784557494786869271', ' com.benbasha.whoopeecushion'),
(' com.airplaneflighttakeoff', '115981152169430603941'),...]
但我想始终将数字id作为元组的第一个元素。请注意,这不是在示例中显示的列表的最后一个元素上发生的。
我该怎么做?稍后我需要遍历边缘,并且需要考虑边缘的顺序,这意味着我需要元组的第一个元素始终是数字id。
问题是在阅读图形时或完成图形后如何实现?
最佳答案
一种想法是使用str.isdigit
测试节点是否为数字。 (例如,请参见this SO answer。)然后,您可以创建边列表,对每个边进行排序,以使数字节点排在最前面:
edges = []
for u, v, d in G.edges(data=True): # note that d contains each edge's data
if u.isdigit(): # if u is numeric put it first
edges.append( (u, v, d) )
else:
edges.append( (v, u, d) )
或采用单线形式:
edges = [ (u, v, d) if u.isdigit() else (v, u, d) for u, v, d in G.edges(data=True) ]
print edges
输出:
[('117923818995099650007', ' air.com.agg.popcornmakermarket', {'weight': -1.0}),
('104000841215686444437', ' air.com.zahdoo.cadie', {'weight': 1.0}),
('115564928125997351000', ' ah.creativecodeapps.tiempo', {'weight': 1.0})]
关于python - 阅读二部图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33513935/