Keyword bipartite
ในวิดีโอ Eric ได้แนะนำ keyword 'bipartite' ให้รู้จักกัน keyword นี้เป็นส่วนหนึ่งของ metadata dictionary ของโหนด และสามารถกำหนดค่าได้ทั้งตอนที่เพิ่มโหนด และหลังจากเพิ่มโหนดแล้ว อย่างไรก็ตาม โปรดจำไว้ว่าตามนิยามของกราฟ bipartite โหนดไม่สามารถเชื่อมต่อกับโหนดอื่นใน partition เดียวกันได้
ในแบบฝึกหัดนี้ จะได้เขียนฟังก์ชันที่คืนค่าโหนดจาก partition ที่กำหนดในกราฟ bipartite โดย partition ที่เกี่ยวข้องในกราฟ bipartite ของ GitHub ที่จะใช้งานคือ 'projects' และ 'users'
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การวิเคราะห์เครือข่ายระดับกลางใน Python
คำแนะนำการฝึกหัด
- เขียนฟังก์ชันชื่อ
get_nodes_from_partition()ที่รับอาร์กิวเมนต์ 2 ตัว ได้แก่ กราฟ bipartiteGและpartitionของGแล้วคืนค่าเฉพาะโหนดจากpartitionนั้น- วนซ้ำผ่านโหนดทั้งหมดของ
G(โดยไม่รวม metadata) โดยใช้ลูปfor - เข้าถึง keyword
'bipartite'ใน metadata dictionary ของโหนดปัจจุบัน ถ้าค่าเท่ากับpartitionให้เพิ่มโหนดปัจจุบันลงในลิสต์nodes
- วนซ้ำผ่านโหนดทั้งหมดของ
- ใช้ฟังก์ชัน
get_nodes_from_partition()ร่วมกับฟังก์ชันlen()เพื่อ:- แสดงจำนวนโหนดใน partition
'projects'ของG - แสดงจำนวนโหนดใน partition
'users'ของG
- แสดงจำนวนโหนดใน partition
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define get_nodes_from_partition()
def ____:
# Initialize an empty list for nodes to be returned
nodes = []
# Iterate over each node in the graph G
for n in ____:
# Check that the node belongs to the particular partition
if G.nodes[n]['____'] == ____:
# If so, append it to the list of nodes
____
return nodes
# Print the number of nodes in the 'projects' partition
print(____(get_nodes_from_partition(____, '____')))
# Print the number of nodes in the 'users' partition
print(____(____))