我正在創建一個做一些簡單的圖論計算的類,我在代碼的底部創建了一個字典,我想知道如何在類中使用字典?Python:創建使用字典的類
我想嘗試這樣的事情,
k = Graph(d.keys())
,但它沒有工作,它給了我這個錯誤
TypeError: __init__() takes exactly 3 arguments (2 given)
沒有任何人有一個建議嗎?
class Graph:
"""docstring"""
def __init__(self, node, degree):
self.node = node
self.degree = degree
def Add(self):
return sum(node)
if __name__ == '__main__':
infile = raw_input("Enter File Name: ")
myfile = open(infile, 'r')
d = {}
with myfile as f:
next(f)
for line in f:
k,v = line.split()
d[int(k)] = int(v)
next(f)
k = d.keys()
v = d.values()
你想爲每一行創建一個'Graph()'對象嗎?當您將'd.keys()'傳遞給'Graph()'構造函數時,您期望發生什麼並不清楚。 –
讀取文件時包含什麼樣的數據?您需要向我們提供更多具體信息,瞭解您希望看到的輸入內容和輸出內容。 –
它不應該是'Add'方法中的'return sum(self.node)'嗎? – iCodez