จัดเรียงโหนดกราฟตามระดับ

ฉันมีกราฟ G ใน networkx และต้องการเรียงลำดับโหนดตามระดับ อย่างไรก็ตาม รหัสต่อไปนี้ใช้ไม่ได้กับ networkx เวอร์ชันล่าสุด:

sorted(set(G.degree().values()))

และสิ่งต่อไปนี้ดูค่อนข้างจะเทอะทะเล็กน้อยเนื่องจากต้องแปลง networkx DegreeView เป็นรายการ tuples ของ python

degrees = [(node,val) for (node, val) in G.degree()]
sorted(degrees, key=lambda x: x[1], reverse=True)

มีวิธีที่ดีกว่านี้ไหม?


person famargar    schedule 22.01.2018    source แหล่งที่มา


คำตอบ (1)


ผลงานดังต่อไปนี้:

sorted(G.degree, key=lambda x: x[1], reverse=True)
person rodgdor    schedule 22.01.2018
comment
คุณคือฮีโร่ที่แท้จริง - person E.Serra; 05.12.2019