簡體   English   中英

使用python將兩個列表列表拆分為子圖

[英]Split two lists of lists into subgraphs using python

我有一個網絡作為列表列表,其中第一個列表是源節點,第二個列表是目標節點,然后兩個列表結合起來告訴您哪些源對哪些目標有優勢。

所以基本上我有這個:

edge_index = [[0,1,2,3,5,6,5,9,10,11,12,12,13],[1,2,3,4,6,7,8,10,11,10,13,12,9]]

我想將此列表結構拆分為:

[[0,1,2,3,5,6,5],[9,10,11,12,12,13]]
[[1,2,3,4,6,7,8],[10,11,10,13,12,9]]

即8 和9 之間沒有聯系,所以它是一個新的子圖。

我不能使用networkx,因為它似乎沒有給我正確數量的子圖(我知道應該提前有多少個網絡)。 所以我想用不同的方法對列表進行子圖化,然后看看我得到的數字是否與 NetworkX 相同。

我寫了這段代碼:

edge_index = [[0,1,2,3,5,6,5],[1,2,3,4,6,7,8]]
origins_split = edge_index[0]
dest_split = edge_index[1]

master_list_of_all_graph_nodes = [0,1,2,3,4,5,6,7,8]  ##for testing
list_of_graph_nodes = []
list_of_origin_edges = []
list_of_dest_edges = []

graph_nodes = []
graph_edge_origin = []
graph_edge_dest = []
targets_list = []


for o,d in zip(origins_split,dest_split): #change
    if o not in master_list_of_all_graph_nodes:
        if d not in master_list_of_all_graph_nodes:
            nodes = [o,d]
            origin = [o]
            dest = [d]
            graph_nodes.append(nodes)
            graph_edge_origin.append(origin)
            graph_edge_dest.append(dest)



        elif d in master_list_of_all_graph_nodes:
            for index,graph_node_list in enumerate(graph_nodes):
                if d in graph_node_list:
                    origin_list = graph_edge_origin[index]
                    origin_list.append(o)
                    dest_list.append(d)
                    master_list_of_all_graph_nodes.append(o)
    


    if d not in master_list_of_all_graph_nodes:
        if o in master_list_of_all_graph_nodes:
            for index,graph_node_list in enumerate(graph_nodes):
                if o in graph_node_list:
                    origin_list = graph_edge_origin[index]
                    origin_list.append(o)
                    dest_list.append(d)
                    master_list_of_all_graph_nodes.append(d)


    if o in master_list_of_all_graph_nodes:
        if d in master_list_of_all_graph_nodes:

            o_index = ''
            d_index = ''
            for index,graph_node_list in enumerate(graph_nodes):
                if d in graph_node_list:
                    d_index = index
                if o in graph_node_list:
                    o_index = index
            
            if o_index == d_index:
                graph_edge_origin[o_index].append(o)
                graph_edge_dest[d_index].append(d)
                master_list_of_all_graph_nodes.append(o)
                master_list_of_all_graph_nodes.append(d)


            else:
                o_list = graph_edge_origin[o_index]
                d_list = graph_edge_dest[d_index]

                node_o_list = node_list[o_index]
                node_d_list = node_list[d_index]
                new_node_list = node_o_list + node_d_list

                node_list.remove(node_o_list)
                node_list.remove(node_d_list)
                graph_edge_origin.remove(o_list)
                graph_edge_dest.remove(d_list)
                new_origin_list = o_list.append(o)
                new_dest_list = d_list.append(d)
                graph_nodes.append(new_node_list)
                graph_edge_dest.append(new_dest_list)
                graph_edge_origin.append(new_origin_list)
                master_list_of_all_graph_nodes.append(o)
                master_list_of_all_graph_nodes.append(d)

print(graph_nodes)
print(graph_edge_dest)
print(graph_edge_origin)

我得到了錯誤:

    graph_edge_origin[o_index].append(o)
TypeError: list indices must be integers or slices, not str

我想知道是否有人可以證明我哪里出錯了,但我覺得我這樣做的效率真的很低,所以如果有人能證明一個更好的方法,我會很感激。 我可以看到其他類似的問題,但沒有一個我可以具體弄清楚如何在這里申請。

在這一行:

graph_edge_origin[o_index].append(o)

o_index 是一個字符串(可能是空字符串,因為沒有輸入 for 循環)。

通常,要么在失敗的行上設置斷點並檢查調試器中的變量,要么在失敗的行之前打印出變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM