Queries on a graph
Now that you know some basic properties of the graph and have practiced using NetworkX's drawing facilities to visualize components of it, it's time to explore how you can query it for nodes and edges. Specifically, you're going to look for "nodes of interest" and "edges of interest". To achieve this, you'll make use of the .nodes() and .edges() methods that Eric went over in the video. The .nodes() method returns a Node view iterable, while the .edges() method returns an edge view iterable, in which each tuple shows the nodes that are present on that edge. Recall that passing in the keyword argument data=True in these methods retrieves the corresponding metadata associated with the nodes and edges as well.
You'll write list comprehensions to effectively build these queries in one line. For a refresher on list comprehensions, refer to Part 2 of DataCamp's Python Data Science Toolbox course. Here's the recipe for a list comprehension:
[ output expression for iterator variable in iterable if predicate expression ].
You have to fill in the _iterable_ and the _predicate expression_. Feel free to prototype your answer by exploring the graph in the IPython Shell before submitting your solution.
This exercise is part of the course
Introduction to Network Analysis in Python
Exercise instructions
- Use a list comprehension to get a list of nodes from the graph
Tthat have the'occupation'label of'scientist'.- The output expression
nhas been specified for you, along with the iterator variablesnandd. Your task is to fill in the iterable and the conditional expression. - Use the
.nodes()method ofTaccess its nodes, and be sure to specifydata=Trueto obtain the metadata for the nodes. - The iterator variable
dis a dictionary. The key of interest here is'occupation'and value of interest is'scientist'.
- The output expression
- Use a list comprehension to get a list of edges from the graph
Tthat were formed for at least 6 years, i.e., from before 1 Jan 2010.- Your task once again is to fill in the iterable and conditional expression.
- Use the
.edges()method ofTto access its edges. Be sure to obtain the metadata for the edges as well. - The dates are stored as
datetime.dateobjects in the metadata dictionaryd, under the key'date'. To access the date 1 Jan 2009, for example, the dictionary value would bedate(2009, 1, 1).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use a list comprehension to get the nodes of interest: noi
noi = [n for n, d in ____ if d['____'] == '____']
# Use a list comprehension to get the edges of interest: eoi
eoi = [(u, v) for u, v, d in ____ if ____ < ____]