0
我試圖使用列表理解將列表的矩陣列表中的所有值轉換爲整數。以下是代碼的程序中包含由空格分隔號線的一個txt文件中讀取一個片段:試圖使用列表理解將字符串列表轉換爲整數?
def readMatrix(file):
contents = open(file).read()
return [item.split() for item in contents.split("\n")]
return [int(item) for item in contents]
當我嘗試做的名單算術在我的程序的其他功能我得到:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
請出示實際的代碼。程序中沒有'+'。 – thefourtheye 2014-10-18 16:22:20
函數中的第二個'return'語句將被完全忽略。你不在函數的任何地方使用'+'。 – 2014-10-18 16:22:22
你的第一個'return'語句產生一個帶有* strings *的列表列表。 – 2014-10-18 16:22:45