我是新手編程,Python和networkx(ouch!)並嘗試將四個graphml文件合併爲一個並刪除重複節點,遵循優秀指令here將幾個graphml文件合併到networkx中並刪除重複項
但是,我不知道如何跟蹤重複節點,當有四個文件比較,而不是兩個。我在下面寫的代碼不起作用,但你可以希望看到我在想我的錯誤,並幫助我。
# script to merge one or more graphml files into one single graphml file
# First read graphml-files into Python and Networkx (duplicate variables as necessary)
A = nx.read_graphml("file1.graphml")
B = nx.read_graphml("file2.graphml")
C = nx.read_graphml("file3.graphml")
D = nx.read_graphml("file4.graphml")
# Create a new graph variable containing all the previous graphs
H = nx.union(A,B,C,D, rename=('1-','2-','3-','4-'))
# Check what nodes are in two or more of the original graphml-files
duplicate_nodes_a_b = [n for n in A if n in B]
duplicate_nodes_b_c = [n for n in B if n in C]
duplicate_nodes_c_d = [n for n in C if n in D]
all_duplicate_nodes = # How should I get this?
# remove duplicate nodes
for n in all_duplicate nodes:
n1='1-'+str(n)
n2='2-'+str(n)
n3='3-'+str(n)
n4='4-'+str(n)
H.add_edges_from([(n1,nbr)for nbr in H[n2]]) # How can I take care of duplicates_nodes_b_c, duplicates_nodes_c_d?
H.remove_node(n2)
# write the merged graphml files-variable into a new merged graphml file
nx.write.graphml(H, "merged_file.graphml", encoding="utf-8", prettyprint=True)
如果您有合併2的工作方式,則可以將它成對應用,然後應用到合併圖。至於合併重複列表:'duplicates_a_b = set(n代表n中的n,如果n代表B中的)'等等,那麼'all_duplicates = duplicates_a_b | duplicates_b_c | ...'。 – drevicko