我有存儲在數據庫中的加權邊緣列表。 我怎樣才能輕鬆地創建成爲一個圖形(沒有在文件中寫它和閱讀它)來自數據庫加權邊緣列表的圖形
這裏reproductible
import sqlite3
con = sqlite3.connect(":memory:")
with con:
cur = con.cursor()
cur.execute("CREATE TABLE DATEN(Source TEXT, Target TEXT, Weight REAL)")
cur.execute("INSERT INTO DATEN VALUES('X33', 'X20', 0.014)")
cur.execute("INSERT INTO DATEN VALUES('X32', 'X20', 0.024)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X20', 0.167)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X32', 0.015)")
cur.execute("INSERT INTO DATEN VALUES('X32', 'X33', 0.003)")
cur.execute("INSERT INTO DATEN VALUES('X23', 'X33', 0.035)")
cur.execute('SELECT * FROM DATEN')
data = cur.fetchall()
我試圖創建一個圖形失敗:
import networkx as nx
G = nx.Graph()
for x in data:
x1 = {'source': data[0][0], 'target': data[0][1], 'weight': data[0][2]}
print x1
G.add_edge(x1) # THIS IS NOT WORKING
有沒有更簡單的方法來做到這一點?
謝謝!我沒有意識到edges_from可以將光標作爲參數。 – user1043144