Frage

I'm working on a group project and we need to create a list of all the values from a specific node attribute in the graph we are working on. Each node has 6 attributes, we just need a list of all the values for one of our attributes.

import networkx as nx
import scipy as sp
import matplotlib.pyplot as plt
%matplotlib inline 
import urllib

url = "http://josquin.cti.depaul.edu/~rburke/courses/s14/fmh.graphml"
sock = urllib.urlopen(url)  # open URL
fmh = nx.read_graphml(sock)

for i in fmh:
    if fmh.node[i]['Race'] == 'Asian':
        fmh.add_node(i, RaceN=0)
    elif fmh.node[i]['Race'] == 'Black':
        fmh.add_node(i, RaceN=1)
    elif fmh.node[i]['Race'] == 'Hisp':
        fmh.add_node(i, RaceN=2)
    elif fmh.node[i]['Race'] == 'NatAm':
        fmh.add_node(i, RaceN=3)
    elif fmh.node[i]['Race'] == 'Other':
        fmh.add_node(i, RaceN=4)
    elif fmh.node[i]['Race'] == 'White':
        fmh.add_node(i, RaceN=5)

for i in fmh:
    if fmh.node[i]['Sex'] == 'F':
        fmh.add_node(i, SexN=0)
    elif fmh.node[i]['Sex'] == 'M':
        fmh.add_node(i, SexN=1)

I think that's everything pertinent. The pre loaded data has 5 attributes, we added a 6th, we just want to be able to grab the values for a specific attribute, like say make a list of all the values for the RaceN attribute.

The idea is that we have a list of integers which we can call the sp.bincount() function on.

War es hilfreich?

Lösung

Your code won't run without more info. I think you want something like the function networkx.get_node_attributes():

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: G.add_node(1,profit=17)

In [4]: G.add_node(2,profit=42)

In [5]: a = nx.get_node_attributes(G,'profit')

In [6]: a.items()
Out[6]: [(1, 17), (2, 42)]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top