0
我需要確定一個文本文件中的一組路徑在我所做的鄰接矩陣上是否有效。將字符串轉換爲索引列表
myList = []
myList.append([])
myList.append([])
myList.append([])
myList.append([])
myList.append([])
myList[0] = [False,True,False,False,True]
myList[1] = [True,False,True,True,False]
myList[2] = [False,True,False,True,True]
myList[3] = [False,True,True,False,True]
myList[4] = [True,False,True,True,False]
myFile = open("numbers.txt","r+")
def admatrix(myList, myFile):
for nextline in myFile:
nextline = [int(x)-1 for x in list(myFile.strip())]
for a,b in zip(path, path[1:]):
if not myList[a][b]:
return False
else:
return True
print admatrix(myFile, myList)
這是當前代碼我有,但在這條線
nextline = [int(x)-1 for x in list(myFile.strip())]
我不斷收到錯誤消息:
AttributeError: 'list'object has no attribute 'strip'
我該如何解決這個問題,我是一個新手,這樣我可能會完全錯誤,任何幫助將不勝感激。
您切換參數 –
'打印admatrix(MYFILE,myList中)''VS高清admatrix(myList中,MYFILE):' –
此外,更改的'nextline'內循環就沒什麼價值,所以你不知道你在做什麼。如果你想要整數而不是布爾值,那麼用數字開始你的列表 –