Time filter on edges
You're now going to practice filtering the graph using a conditional as applied to the edges. This will help you gain practice and become comfortable with list comprehensions that contain conditionals.
To help you with the exercises, remember that you can import datetime objects from the datetime module. On the graph, the metadata has a date key that is paired with a datetime object as a value.
Deze oefening maakt deel uit van de cursus
Intermediate Network Analysis in Python
Oefeninstructies
- Instantiate a new graph called
G_sub. - Add nodes from the original graph (including the node metadata), using the
.add_nodes_from()method. - Add edges using a list comprehension with one conditional on the edge dates, that the date of the edge is earlier than 2004-05-16. To do this:
- Use the
.add_edges_from()method with a list comprehension as the argument. - The output expression of the list comprehension is
(u, v, d). Iterate over all the edges ofGand check whetherd['date']is less thandatetime(2004, 5, 16).
- Use the
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
import networkx as nx
from datetime import datetime
# Instantiate a new graph: G_sub
G_sub = ____
# Add nodes from the original graph
____
# Add edges using a list comprehension with one conditional on the edge dates, that the date of the edge is earlier than 2004-05-16.
G_sub.____([(____, ____, ____) for u, v, d in G.edges(data=True) if d['____'] < ____(____,____,____)])