简体   繁体   English

Python - 'NoneType'对象不是可下载的错误

[英]Python - 'NoneType' Object is not subscriptable error

I am beginner in Python and have been facing this problem for some time. 我是Python的初学者,并且已经面临这个问题一段时间了。 Any suggestions to correct the problem would be valuable. 任何纠正问题的建议都是有价值的。 Below is the code used to find the shortest path, using Dijkstra's algorithm. 下面是使用Dijkstra算法找到最短路径的代码。 I have the cost matrix and the source as Input. 我有成本矩阵和源作为输入。

def dijkstra(cost_matrix,source):
    n = 20
    dist = [0 for row in range(20)]
    visited = [0 for row in range(20)]
    for j in range(20):
        visited[j] = "False"
    visited[source] = "True"
    for i in range(20):
        dist[i] = cost_matrix[source][i]
    prev_min = source
    for k in range(20):
        minimum = min(dist)
        minimum2 = min(minimum,n)
        for i in range(20):
            if dist[i] > (cost_matrix[prev_min][minimum2] + cost_matrix[minimum2][i]):
                dist[i] = cost_matrix[prev_min][minimum2] + cost_matrix[minimum2][i]
        visited[minimum2] = "True"
        prev_min = minimum2
    return dist  

Code that calculates the cost_matrix 计算cost_matrix的代码

def matrix():
    k = 30
    b_ij = [[0 for row in range(20)] for col in range(20)]
    a_ij = [[0 for row in range(20)] for col in range(20)]
    cost_matrix = [[0 for row in range(20)] for col in range(20)]
    for i in range(20):
        for j in range(20):
            rand = random.randint(0,8)
            b_ij[i][j] = rand
            b_ij[j][i] = rand
    while(k > 0):
        rand1 = random.randint(0,19)
        rand2 = random.randint(0,19)
        a_ij[rand1][rand2] = 200
        a_ij[rand2][rand1] = 200
        k = k - 1
    for i in range(20):
        for j in range(20):
            if a_ij[i][j] != 200:
                rand = random.randint(0,8)
                a_ij[i][j] = rand
                a_ij[j][i] = rand
    for i in range(len(a_ij)):
        for j in range(len(b_ij[0])):
            for k in range(len(b_ij)):
                cost_matrix[i][j] += a_ij[i][k] * b_ij [k][j]

Error Message: 错误信息:

Traceback (most recent call last):
  File "ass1.py", line 60, in <module>
    print(dijkstra(cost_matrix, 1))
  File "ass1.py", line 45, in dijkstra
    dist[i] = cost_matrix[source][i]
TypeError: 'NoneType' object is not subscriptable

You still haven't posted the actual code where you create the cost_matrix . 您仍未发布创建cost_matrix的实际代码。 You posted the code for the matrix function, but not the code where you use the matrix function. 您发布了矩阵函数的代码,但没有发布使用矩阵函数的代码。

However, it looks like the problem is that your matrix function doesn't return anything. 但是,看起来问题是你的matrix函数没有返回任何东西。 It just creates a matrix and throws it away. 它只是创建一个矩阵并抛弃它。 You need to add return cost_matrix at the end of that function. 您需要在该函数的末尾添加return cost_matrix

Either cost_matrix or cost_matrix[source] has the value None, but it should have been a list. cost_matrix或cost_matrix [source]的值都为None,但它应该是一个列表。

Print both of them just before the line: 在行之前打印它们:

dist[i] = cost_matrix[source][i]

to find out what the issue is. 找出问题所在。

By the way, you should probably use numpy for this sort of stuff. 顺便说一句,你应该使用numpy这种东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM