第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果它們不在預(yù)選列表中,如何遍歷圖中的邊

如果它們不在預(yù)選列表中,如何遍歷圖中的邊

動(dòng)漫人物 2022-07-26 21:02:54
我正在過(guò)濾邊的子集,以便可以遍歷它們。在這種情況下,我排除了“末端邊緣”,它們是沿鏈的最終邊緣:import networkx as nxgraph = nx.Graph()graph.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4)])end_nodes = [n for n in graph.nodes if nx.degree(graph, n) == 1]end_edges = graph.edges(end_nodes)print(f"end edges: {end_edges}")for edge in graph.edges:    if edge not in end_edges:        print(f"edge {edge} is not an end edge.")    else:        print(f"edge {edge} is an end edge.")但是,當(dāng)您運(yùn)行此代碼時(shí),您會(huì)得到以下輸出:end edges: [(0, 1), (4, 3)]edge (0, 1) is an end edge.edge (1, 2) is an end edge.edge (2, 3) is an end edge.edge (3, 4) is an end edge.Edges (1, 2)and (2, 3)are not in end_edges,但它在檢查False條件時(shí)返回edge not in end_edges(似乎暗示它實(shí)際上被包含,而它似乎不包含)。發(fā)生了什么事,我該如何正確過(guò)濾?Python 版本是 3.7,NetworkX 是 2.4。
查看完整描述

2 回答

?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以將 end_nodes 轉(zhuǎn)換為一組邊并保持邊無(wú)序。


>>> graph = nx.Graph()

>>> graph.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4)])

>>> end_nodes = [n for n in graph.nodes if nx.degree(graph, n) == 1]

>>> end_edges = set(map(frozenset, graph.edges(end_nodes)))

>>> end_edges

{frozenset({3, 4}), frozenset({0, 1})}

>>> for edge in graph.edges:

...     print(edge, frozenset(edge) in end_edges)

... 

(0, 1) True

(1, 2) False

(2, 3) False

(3, 4) True


查看完整回答
反對(duì) 回復(fù) 2022-07-26
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

import networkx as nx


graph = nx.Graph()

graph.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 4)])

end_nodes = [n for n in graph.nodes if nx.degree(graph, n) == 1]

end_edges = graph.edges(end_nodes)

print(f"end edges: {end_edges}")

for edge in graph.edges:

    if edge not in list(end_edges):

        print(f"edge {edge} is not an end edge.")

    else:

        print(f"edge {edge} is an end edge.")


查看完整回答
反對(duì) 回復(fù) 2022-07-26
  • 2 回答
  • 0 關(guān)注
  • 85 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)