2016-02-11 115 views
0

我有這個python代碼。有人能告訴我'nodedef'和'edgedef'的條件是什麼,需要滿足條件是什麼規則。python代碼含義解釋

我嘗試谷歌的答案,但我沒有找到任何有用的東西。我是python的新手,所以請原諒我的無知。

import numpy 
import pandas 
import networkx as nx 
import unicodecsv as csv 

path="comics-network.csv" 

graph = nx.Graph(name="Characters in Comics") 
with open(path, 'rb') as data: 
    reader = csv.reader(data) 
    for row in reader: 
     if 'nodedef' in row[0]: 
      handler = lambda row,G: G.add_node(row[0],TYPE=row[1]) 
     elif 'edgedef' in row[0]: 
      handler = lambda row,G: G.add_edge(*row) 
     else: 
      handler=(row, graph) 
+0

他們是字符串。它看起來像你正在閱讀的任何文件都會將它們放在文本中。 –

+0

您正在加載.csv文件;對於每一行,如果第一個項目是「nodedef」將其視爲一個節點,否則如果它是「edgedef」將其視爲邊緣,否則??我不確定最後一點在做什麼。應該有更多的代碼,顯示'handler'創建後會發生什麼。 –

+0

該文件是什麼樣子的? – zondo

回答

0

我會嘗試猜測,一行註釋行:

# for each row in the CSV 
    for row in reader: 
     # if the first column of the row contains the string 'nodedef' 
     if 'nodedef' in row[0]: 
      # make the name "handler" point to this function (node?) 
      handler = lambda row,G: G.add_node(row[0],TYPE=row[1]) 
     # else, check if it contains the string 'edgedef' 
     elif 'edgedef' in row[0]: 
      # make the name "handler" point to this function instead (edge?) 
      handler = lambda row,G: G.add_edge(*row) 
     else: 
      # make the name "handler" point to this tuple (root?) 
      handler=(row, graph) 

我相信它是幹什麼用「處理」之後的東西。

0

看起來這是一些代碼,構建了基於在圖上(例如adjacency list)的人類可讀表示的graph的內存中表示的開始。

我懷疑你的CSV使用字符串的'nodedef'來表示下面的數據就是指圖中的一個節點,'edgedef'表示下面的數據指向圖上的邊。