按天找出最受欢迎的论坛: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