BaşlayınÜcretsiz Başlayın

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.

Bu egzersiz

Intermediate Network Analysis in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • 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 of G and check whether d['date'] is less than datetime(2004, 5, 16).

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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['____'] < ____(____,____,____)])
Kodu Düzenle ve Çalıştır