I have a knowledge base graph (built in networkx
).
Here is made up example.
G = nx.DiGraph()
G.add_edge('user1', 'New York', relation = 'works at')
G.add_edge('user1', 'Boston', relation = 'from')
G.add_edge('user1', '27', relation = 'Age')
Between each pair of nodes I have specific relation such as 'works at', 'from', etc
I want to check if I have desired edge for specific node. For example, do I know information about where user1 works at.
Currently, I do it in a loop:
for connected_node, attributes in G['user1'].items():
if attributes['relation'] == 'works at':
print(True, connected_node)
Is it possible to check if a node has specific edge without loop?
And consequently get connected node by this edge?