開始使用免費開始

推薦儲存庫

就快到尾聲了!這題要你練習使用集合的差集運算,並把它用在從第二位使用者挑出第一位使用者應該貢獻的儲存庫。

本練習屬於課程

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(____)
編輯並執行程式碼