开始使用免费开始使用

推荐仓库

就快结束了!本题将练习集合差集的用法,并把它应用到推荐第二位用户的仓库,让第一位用户去参与贡献。

本练习是课程的一部分

Python 网络分析中级

查看课程

练习说明

  • 编写一个名为 recommend_repositories() 的函数,接收 3 个参数 Gfrom_userto_user,返回这样一组仓库:from_user 已关联、而 to_user 未关联的仓库。
    • 获取 from_user 参与贡献过的仓库集合,存为 from_repos。为此,先获取 from_user 的邻居,然后对其使用 set()
    • 获取 to_user 参与贡献过的仓库集合,存为 to_repos
    • 使用 .difference() 方法,返回 from_user 已关联、而 to_user 未关联的仓库。
  • 打印需要 'u7909' 推荐给 'u2148' 的仓库。

交互式实操练习

通过完成这段示例代码来试试这个练习。

def ____:
    # Get the set of repositories that from_user has contributed to
    from_repos = ____
    # Get the set of repositories that to_user has contributed to
    to_repos = ____

    # Identify repositories that the from_user is connected to that the to_user is not connected to
    return ____.____(____)

# Print the repositories to be recommended
print(____)
编辑并运行代码