def parse_p2g(lines):
 description = next(lines).strip()
 G = networkx.MultiDiGraph(name=description, selfloops=True)
 (nnodes, nedges) = map(int, next(lines).split())
 nodelabel = {}
 nbrs = {}
 for i in range(nnodes):
  n = next(lines).strip()
  nodelabel[i] = n
  G.add_node(n)
  nbrs[n] = map(int, next(lines).split())
 for n in G:
  for nbr in nbrs[n]:
   G.add_edge(n, nodelabel[nbr])
 return G

def parse_p2g ( lines ) : description = next ( lines ) . strip ( ) G = networkx . MultiDiGraph ( name = description , selfloops = True ) ( nnodes , nedges ) = map ( int , next ( lines ) . split ( ) ) nodelabel = { } nbrs = { } for i in range ( nnodes ) : n = next ( lines ) . strip ( ) nodelabel [ i ] = n G . add_node ( n ) nbrs [ n ] = map ( int , next ( lines ) . split ( ) ) for n in G : for nbr in nbrs [ n ] : G . add_edge ( n , nodelabel [ nbr ] ) return G
parse p2g format graph from string or iterable .