I create a graph with networkx and every node has some attributes. So i want to search all nodes for a specific attributes and save every node who has this attribute in a list. I wrote the following code but im getting an error:
for node in G.nodes():
for attribute in G.node[node]['attributes']:
if attribute in question:
setOfUsers.append(node)
With this code im getting the following error:
for attribute in G.node[node]['attributes']:
KeyError: 'attributes'
So i search the forum and i tried something different to fix the problem:
for node, data in G.nodes(data=True):
if data['attributes'] == question[0]:
setOfUsers.append(node)
but i have the same error. How can iterate through the attributes?
Update: I add the node attributes with the code below. I read the attributes from a file, i split commas and newline character and then i save list in nodes
for line in file2:
line = line.strip()
words = line.split('\t')
node = int(words[0])
attributes= words[1]
splittedAttributes = attributes.split(',')
if node in G.nodes():
G.node[node]['attributes'] = splittedAttributes
skillsattribute to the graph. - Tai'attribute'attribute? Ifnode in G.nodes()is never true, in the lineif node in G.nodes(): G.node[node]['attributes'] = splittedAttributesthen nonodewill be assigned an'attributes'attribute. - unutbuG.nodes(data=True)for us? - Tai