简体   繁体   中英

Python - 'NoneType' Object is not subscriptable error

I am beginner in Python and have been facing this problem for some time. Any suggestions to correct the problem would be valuable. Below is the code used to find the shortest path, using Dijkstra's algorithm. 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

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 . 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. It just creates a matrix and throws it away. You need to add return cost_matrix at the end of that function.

Either cost_matrix or cost_matrix[source] has the value None, but it should have been a list.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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