Here is the "one-liner" way ([17])
In [15]: G=nx.fast_gnp_random_graph(10,0.1)
In [16]: G.degree()
Out[16]: {0: 4, 1: 3, 2: 3, 3: 2, 4: 1, 5: 2, 6: 4, 7: 2, 8: 1, 9: 2}
In [17]: G.remove_nodes_from((n for n,d in G.degree_iter() if d==1))
In [18]: G.degree()
Out[18]: {0: 4, 1: 2, 2: 2, 3: 2, 5: 2, 6: 4, 7: 2, 9: 2}
Aric
Amitabh
> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>
Hi Amitabh,
It's nearly identical to what Aric showed you. Previously you wanted to
remove all nodes which had degree equal to 1. Now you want to remove
all nodes which have degree not equal to 2. So replace,
d==1
with
d!=2
Chris
G.remove_nodes_from((n for n in G if n not in G[friend1] or n not in G[friend2]))
But its probably better to do something like:
bothnbrs=set([friend1,friend2]) ^ (set(G[friend1]) & set(G[friend2]))
H=G.subgraph(bothnbrs)
Dan
cheers