“'NoneType' 对象不可订阅”错误
"'NoneType' object is not subscriptable" error
我是 python 的新手,我对这段代码有疑问。我不明白为什么我不能 运行 代码:print(m[1][1])
我总是收到此错误消息:TypeError: 'NoneType' object is not subscriptable
edges = [(1,2), (2,7), (1,3), (2,4), (4,7), (3,5), (4,5), (5,6), (6,7), (1,8), (5,8), (6,9), (7,9), (9,10), (5,10), (8,10)]`
def generateAdjMatrix(edges):
max_knoten = max(max(edges))
matrix = [[0 for i in range(max_knoten)] for j in range(max_knoten)]
for kante in edges:
matrix[kante[0]-1][kante[1]-1] = 1
pass
if __name__ == '__main__':
m = generateAdjMatrix(edges)
print(m[1][1])
pass
m
在 m = generateAdjMatrix(edges)
之后是 None
因为 generateAdjMatrix
没有明确地 return 任何东西。
请参阅文档中的 Defining Functions:
Coming from other languages, you might object that fib
is not a function but a procedure since it doesn’t return a value. In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None
(it’s a built-in name).
m = generateAdjMatrix(edges)
你正在调用这个函数,这个函数是用这个函数体声明的:
max_knoten = max(max(edges))
matrix = [[0 for i in range(max_knoten)] for j in range(max_knoten)]
for kante in edges:
matrix[kante[0]-1][kante[1]-1] = 1
你看到 return
了吗?不,因为你的函数相当于 C
/C++
void
函数。
那么 m
中放入了什么?无,在Python中是None
,type
NoneType
的对象。
一般来说,在 Python 中编程时,如果您收到这样的错误消息:
TypeError: 'NoneType' object is not subscriptable
你必须检查你下标的变量是从哪里来的。
我是 python 的新手,我对这段代码有疑问。我不明白为什么我不能 运行 代码:print(m[1][1])
我总是收到此错误消息:TypeError: 'NoneType' object is not subscriptable
edges = [(1,2), (2,7), (1,3), (2,4), (4,7), (3,5), (4,5), (5,6), (6,7), (1,8), (5,8), (6,9), (7,9), (9,10), (5,10), (8,10)]`
def generateAdjMatrix(edges):
max_knoten = max(max(edges))
matrix = [[0 for i in range(max_knoten)] for j in range(max_knoten)]
for kante in edges:
matrix[kante[0]-1][kante[1]-1] = 1
pass
if __name__ == '__main__':
m = generateAdjMatrix(edges)
print(m[1][1])
pass
m
在 m = generateAdjMatrix(edges)
之后是 None
因为 generateAdjMatrix
没有明确地 return 任何东西。
请参阅文档中的 Defining Functions:
Coming from other languages, you might object that
fib
is not a function but a procedure since it doesn’t return a value. In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is calledNone
(it’s a built-in name).
m = generateAdjMatrix(edges)
你正在调用这个函数,这个函数是用这个函数体声明的:
max_knoten = max(max(edges))
matrix = [[0 for i in range(max_knoten)] for j in range(max_knoten)]
for kante in edges:
matrix[kante[0]-1][kante[1]-1] = 1
你看到 return
了吗?不,因为你的函数相当于 C
/C++
void
函数。
那么 m
中放入了什么?无,在Python中是None
,type
NoneType
的对象。
一般来说,在 Python 中编程时,如果您收到这样的错误消息:
TypeError: 'NoneType' object is not subscriptable
你必须检查你下标的变量是从哪里来的。