1

I'm trying to read a graph from a .txt file that has the following structure:

115564928125997351000, ah.creativecodeapps.tiempo,1
117923818995099650007, air.com.agg.popcornmakermarket,-1
104000841215686444437, air.com.zahdoo.cadie,1
.
.
.

I've been using the following command:

g=nx.read_weighted_edgelist('siatoy.txt', delimiter=',',nodetype=str,encoding='utf-8')

But when I do g.edges(data=True), I got this:

[('106784557494786869271', ' com.map2app.U5635321228165120A5661458385862656'),
('106784557494786869271',' com.jb.gokeyboard.theme.mzdevelopment.americankeyboard'),
('106784557494786869271', ' com.benbasha.whoopeecushion'),
(' com.airplaneflighttakeoff', '115981152169430603941'),...]

But I want to get always the numeric id as the first element of the tuple. Note that this is not happening at the last element of the list I show in the example.

How can I do to get this? I need to iterate over the edges later and I need to take into account the order on the edges, meaning that I need the first element of the tuple to be the numeric id always.

The question is how can I achieve this while reading the graph or after doing it?

mdml
  • 22,442
  • 8
  • 58
  • 66
nhern121
  • 3,831
  • 6
  • 27
  • 40

1 Answers1

1

One idea is to use str.isdigit to test if a node is numeric. (For example, see this SO answer.) Then you could create a list of edges, sorting each edge so that the numeric nodes come first:

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) )

Or in one-liner form:

edges = [ (u, v, d) if u.isdigit() else (v, u, d)  for u, v, d in G.edges(data=True) ]
print edges

This outputs:

[('117923818995099650007', ' air.com.agg.popcornmakermarket', {'weight': -1.0}),
('104000841215686444437', ' air.com.zahdoo.cadie', {'weight': 1.0}),
('115564928125997351000', ' ah.creativecodeapps.tiempo', {'weight': 1.0})]
Community
  • 1
  • 1
mdml
  • 22,442
  • 8
  • 58
  • 66