推薦儲存庫
就快到尾聲了!這題要你練習使用集合的差集運算,並把它用在從第二位使用者挑出第一位使用者應該貢獻的儲存庫。
本練習屬於課程
Python 網路分析進階
練習說明
- 撰寫一個名為
recommend_repositories()的函式,接受 3 個引數G、from_user、to_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(____)