เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Keyword bipartite

ในวิดีโอ Eric ได้แนะนำ keyword 'bipartite' ให้รู้จักกัน keyword นี้เป็นส่วนหนึ่งของ metadata dictionary ของโหนด และสามารถกำหนดค่าได้ทั้งตอนที่เพิ่มโหนด และหลังจากเพิ่มโหนดแล้ว อย่างไรก็ตาม โปรดจำไว้ว่าตามนิยามของกราฟ bipartite โหนดไม่สามารถเชื่อมต่อกับโหนดอื่นใน partition เดียวกันได้

ในแบบฝึกหัดนี้ จะได้เขียนฟังก์ชันที่คืนค่าโหนดจาก partition ที่กำหนดในกราฟ bipartite โดย partition ที่เกี่ยวข้องในกราฟ bipartite ของ GitHub ที่จะใช้งานคือ 'projects' และ 'users'

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การวิเคราะห์เครือข่ายระดับกลางใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • เขียนฟังก์ชันชื่อ get_nodes_from_partition() ที่รับอาร์กิวเมนต์ 2 ตัว ได้แก่ กราฟ bipartite G และ partition ของ G แล้วคืนค่าเฉพาะโหนดจาก partition นั้น
    • วนซ้ำผ่านโหนดทั้งหมดของ G (โดยไม่รวม metadata) โดยใช้ลูป for
    • เข้าถึง keyword 'bipartite' ใน metadata dictionary ของโหนดปัจจุบัน ถ้าค่าเท่ากับ partition ให้เพิ่มโหนดปัจจุบันลงในลิสต์ nodes
  • ใช้ฟังก์ชัน get_nodes_from_partition() ร่วมกับฟังก์ชัน len() เพื่อ:
    • แสดงจำนวนโหนดใน partition 'projects' ของ G
    • แสดงจำนวนโหนดใน partition 'users' ของ G

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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(____(____))
แก้ไขและรันโค้ด