Simple Way for Modifying Attributes of Single nodes in Networkx 2.1+
I'm looking for an simple way of modifying the value of a single attribute of a single node inside a NetworkX Graph.
The NetworkX documentation only mentions a function for setting an attribute for all nodes in the graph, e.g.:
nx.set_node_attributes(G, bb, 'betweenness')
This might be appropriate in many situations in which such such an attribute is easy to calculate for all nodes in a graph (like be mentioned betweenness). Likewise, there is an easy way to access single node attributes in NetworkX:
graph.nodes[nodeName][attribute]
However, the attributes accessed this way are read-only.
So what i'm looking for a way to set attributes as simple as reading.
Thanks in advance.
python-3.x graph networkx
add a comment |
I'm looking for an simple way of modifying the value of a single attribute of a single node inside a NetworkX Graph.
The NetworkX documentation only mentions a function for setting an attribute for all nodes in the graph, e.g.:
nx.set_node_attributes(G, bb, 'betweenness')
This might be appropriate in many situations in which such such an attribute is easy to calculate for all nodes in a graph (like be mentioned betweenness). Likewise, there is an easy way to access single node attributes in NetworkX:
graph.nodes[nodeName][attribute]
However, the attributes accessed this way are read-only.
So what i'm looking for a way to set attributes as simple as reading.
Thanks in advance.
python-3.x graph networkx
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15
add a comment |
I'm looking for an simple way of modifying the value of a single attribute of a single node inside a NetworkX Graph.
The NetworkX documentation only mentions a function for setting an attribute for all nodes in the graph, e.g.:
nx.set_node_attributes(G, bb, 'betweenness')
This might be appropriate in many situations in which such such an attribute is easy to calculate for all nodes in a graph (like be mentioned betweenness). Likewise, there is an easy way to access single node attributes in NetworkX:
graph.nodes[nodeName][attribute]
However, the attributes accessed this way are read-only.
So what i'm looking for a way to set attributes as simple as reading.
Thanks in advance.
python-3.x graph networkx
I'm looking for an simple way of modifying the value of a single attribute of a single node inside a NetworkX Graph.
The NetworkX documentation only mentions a function for setting an attribute for all nodes in the graph, e.g.:
nx.set_node_attributes(G, bb, 'betweenness')
This might be appropriate in many situations in which such such an attribute is easy to calculate for all nodes in a graph (like be mentioned betweenness). Likewise, there is an easy way to access single node attributes in NetworkX:
graph.nodes[nodeName][attribute]
However, the attributes accessed this way are read-only.
So what i'm looking for a way to set attributes as simple as reading.
Thanks in advance.
python-3.x graph networkx
python-3.x graph networkx
edited Nov 27 '18 at 22:19
lowercaseonly
asked Nov 27 '18 at 21:55
lowercaseonlylowercaseonly
33
33
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15
add a comment |
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15
add a comment |
1 Answer
1
active
oldest
votes
In your example, bb
is a dict whose keys are the nodes. You don't need the dict to have a key for all nodes in the graph, just the nodes for which you want to define the attribute. In the example below, I create a graph, and then set the 'weight'
of node 0
to be 5
and of node 3
to be 2
. This leaves the attributes for the other nodes unaffected, so since they've never been created, they don't exist.
import networkx as nx
G = nx.fast_gnp_random_graph(10,0.2)
nx.set_node_attributes(G, {0:5, 3:2}, 'weight')
G.nodes[0]['weight']
> 5
G.nodes[3]['weight']
> 2
G.nodes[1]['weight']
> KeyError: 'weight'
So we set the weight for 0
and 3
but not any of the others. You could also set more than one attribute at once, but that requires a slightly different call. Here we have
nx.set_node_attributes(G, {1:{'weight':-1, 'volume':4}})
G.nodes[1]['weight']
> -1
G.nodes[1]['volume']
> 4
Just to see what the attributes look like after all of that:
G.nodes(data=True)
> NodeDataView({0: {'weight': 5}, 1: {'weight': -1, 'volume': 4}, 2: {}, 3: {'weight': 2}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}})
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508805%2fsimple-way-for-modifying-attributes-of-single-nodes-in-networkx-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example, bb
is a dict whose keys are the nodes. You don't need the dict to have a key for all nodes in the graph, just the nodes for which you want to define the attribute. In the example below, I create a graph, and then set the 'weight'
of node 0
to be 5
and of node 3
to be 2
. This leaves the attributes for the other nodes unaffected, so since they've never been created, they don't exist.
import networkx as nx
G = nx.fast_gnp_random_graph(10,0.2)
nx.set_node_attributes(G, {0:5, 3:2}, 'weight')
G.nodes[0]['weight']
> 5
G.nodes[3]['weight']
> 2
G.nodes[1]['weight']
> KeyError: 'weight'
So we set the weight for 0
and 3
but not any of the others. You could also set more than one attribute at once, but that requires a slightly different call. Here we have
nx.set_node_attributes(G, {1:{'weight':-1, 'volume':4}})
G.nodes[1]['weight']
> -1
G.nodes[1]['volume']
> 4
Just to see what the attributes look like after all of that:
G.nodes(data=True)
> NodeDataView({0: {'weight': 5}, 1: {'weight': -1, 'volume': 4}, 2: {}, 3: {'weight': 2}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}})
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
add a comment |
In your example, bb
is a dict whose keys are the nodes. You don't need the dict to have a key for all nodes in the graph, just the nodes for which you want to define the attribute. In the example below, I create a graph, and then set the 'weight'
of node 0
to be 5
and of node 3
to be 2
. This leaves the attributes for the other nodes unaffected, so since they've never been created, they don't exist.
import networkx as nx
G = nx.fast_gnp_random_graph(10,0.2)
nx.set_node_attributes(G, {0:5, 3:2}, 'weight')
G.nodes[0]['weight']
> 5
G.nodes[3]['weight']
> 2
G.nodes[1]['weight']
> KeyError: 'weight'
So we set the weight for 0
and 3
but not any of the others. You could also set more than one attribute at once, but that requires a slightly different call. Here we have
nx.set_node_attributes(G, {1:{'weight':-1, 'volume':4}})
G.nodes[1]['weight']
> -1
G.nodes[1]['volume']
> 4
Just to see what the attributes look like after all of that:
G.nodes(data=True)
> NodeDataView({0: {'weight': 5}, 1: {'weight': -1, 'volume': 4}, 2: {}, 3: {'weight': 2}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}})
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
add a comment |
In your example, bb
is a dict whose keys are the nodes. You don't need the dict to have a key for all nodes in the graph, just the nodes for which you want to define the attribute. In the example below, I create a graph, and then set the 'weight'
of node 0
to be 5
and of node 3
to be 2
. This leaves the attributes for the other nodes unaffected, so since they've never been created, they don't exist.
import networkx as nx
G = nx.fast_gnp_random_graph(10,0.2)
nx.set_node_attributes(G, {0:5, 3:2}, 'weight')
G.nodes[0]['weight']
> 5
G.nodes[3]['weight']
> 2
G.nodes[1]['weight']
> KeyError: 'weight'
So we set the weight for 0
and 3
but not any of the others. You could also set more than one attribute at once, but that requires a slightly different call. Here we have
nx.set_node_attributes(G, {1:{'weight':-1, 'volume':4}})
G.nodes[1]['weight']
> -1
G.nodes[1]['volume']
> 4
Just to see what the attributes look like after all of that:
G.nodes(data=True)
> NodeDataView({0: {'weight': 5}, 1: {'weight': -1, 'volume': 4}, 2: {}, 3: {'weight': 2}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}})
In your example, bb
is a dict whose keys are the nodes. You don't need the dict to have a key for all nodes in the graph, just the nodes for which you want to define the attribute. In the example below, I create a graph, and then set the 'weight'
of node 0
to be 5
and of node 3
to be 2
. This leaves the attributes for the other nodes unaffected, so since they've never been created, they don't exist.
import networkx as nx
G = nx.fast_gnp_random_graph(10,0.2)
nx.set_node_attributes(G, {0:5, 3:2}, 'weight')
G.nodes[0]['weight']
> 5
G.nodes[3]['weight']
> 2
G.nodes[1]['weight']
> KeyError: 'weight'
So we set the weight for 0
and 3
but not any of the others. You could also set more than one attribute at once, but that requires a slightly different call. Here we have
nx.set_node_attributes(G, {1:{'weight':-1, 'volume':4}})
G.nodes[1]['weight']
> -1
G.nodes[1]['volume']
> 4
Just to see what the attributes look like after all of that:
G.nodes(data=True)
> NodeDataView({0: {'weight': 5}, 1: {'weight': -1, 'volume': 4}, 2: {}, 3: {'weight': 2}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}})
edited Nov 28 '18 at 1:00
answered Nov 27 '18 at 22:42
JoelJoel
10.4k2555
10.4k2555
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
add a comment |
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
Perfect! Thank you very much, solves the problem for me
– lowercaseonly
Nov 27 '18 at 23:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53508805%2fsimple-way-for-modifying-attributes-of-single-nodes-in-networkx-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "efficient" ? Fast ?
– Gabriel Devillers
Nov 27 '18 at 22:06
Sorry, i may have clarified that in advance. I simply mean with writing less code (less then e.g. gather the attributes of all nodes in a dict, modify the only attribute i actually want to modify and then call the mentioned setter ).
– lowercaseonly
Nov 27 '18 at 22:09
Ok, then you could improve your question by showing us the few lines that you currently use to modify a single attribute.
– Gabriel Devillers
Nov 27 '18 at 22:12
Actually, i haven't implemented is this way yet. It's just one way it should work w.r.t. the set_node_attributes function as described in the documentation. But i will alter the question
– lowercaseonly
Nov 27 '18 at 22:15