[英]Keep in a Graph only those edges in a list
我有這個名為edgeKeep的邊(元組)列表,我想在圖中保留它,並刪除所有其他邊。 在我的情況下(因為edgeKeep的構建方式),不能保證edgeKeep邊緣中的節點名稱遵循G.edges中節點名稱的相同順序(邊緣可以是('u','v')
一個並在另一個中顯示為('v','u')
)。 所以我必須這樣做:
G.remove_edges_from(e for e in G.edges if e not in edgesKeep and tuple(reversed(e)) not in edgesKeep)
有沒有辦法讓它更簡單? 我的意思是,以某種方式擺脫tuple(reversed(e)) not in edgesKeep
?
您可以對邊中的元組進行edgesKeep
並執行
G.remove_edges_from(e for e in G.edges if sorted(e) not in edgesKeep)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.