開始使用免費開始

逐日找出最受歡迎的討論區:I

太棒了!你來到最後兩題了——其實可以視為一個長題目。這會小小考驗一下你對 Python 程式設計的記憶力!

我們要在每個時間視窗內,查看有多少討論區拿下「最受歡迎的討論區」這個頭銜。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 建立一個名為 most_popular_forums 的清單,用來存放每天最受歡迎的討論區。
  • 建立一個名為 highest_dcs 的清單,用來存放最受歡迎討論區的度中心性分數。
  • 建立一個新的圖 G_sub,並使用 .add_nodes_from() 從原始圖 G 加入節點。
  • 從原始圖 G 中加入符合條件的邊(條件與前一個練習完全相同)。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import necessary modules
from datetime import timedelta
import networkx as nx
import matplotlib.pyplot as plt

# Instantiate a list to hold the list of most popular forums by day: most_popular_forums
most_popular_forums = ____
# Instantiate a list to hold the degree centrality scores of the most popular forums: highest_dcs
highest_dcs = ____
curr_day = dayone  
td = timedelta(days=1)  

while curr_day < lastday:  
    if curr_day.day == 1: 
        print(curr_day) 
    # Instantiate new graph: G_sub
    G_sub = ____
    
    # Add in nodes from original graph G
    ____
    
    # Add in edges from the original graph G that fulfill the criteria
    G_sub.____([(____, ____, ____) for ____, ____, ____ in ____ if d['____'] >= ____ and d['____'] < ____ + ____])
    
    # CODE CONTINUES ON NEXT EXERCISE
    curr_day += td
編輯並執行程式碼