0

Getting below error when executing the python code given. Tried adding list but it still throws the same error.

---------------------------------------------------------------------------
 
--> 204                 dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j] )
 

TypeError: 'map' object is not subscriptable

Please help in resolving this

V = 4
INF = 99999

def floydWarshall(graph):
    dist = map(lambda i : map(lambda j : j , i) , graph)
    for k in range(V):
        # pick all vertices as source one by one
        for i in range(V):
            for j in range(V):
                dist[i][j] = min(dist[i][j] ,dist[i][k]+ dist[k][j] )
    printSolution(dist)

def printSolution(dist):
    print ("Following matrix shows the shortest distances between every pair of vertices")
    for i in range(V):
        for j in range(V):
            if(dist[i][j] == INF):
                print ("%7s" %("INF"),)
            else:
                print ("%7d\t" %(dist[i][j]),)
            if j == V-1:
                print ("")
                graph = [[0,5,INF,10],
                         [INF,0,3,INF],
                         [INF, INF, 0,   1],
                         [INF, INF, INF, 0]
                        ]
floydWarshall(graph);
toydarian
  • 4,246
  • 5
  • 23
  • 35
nirmal10
  • 265
  • 1
  • 3
  • 11

1 Answers1

0

Replace this line dist = map(lambda i : map(lambda j : j , i) , graph)

with this: dist = list(map(lambda i : map(lambda j : j , i) , graph))

Because the map() function is just an iterable, it does nothing until you "consume" it.(https://docs.python.org/3/library/functions.html#map)

bb1950328
  • 1,403
  • 11
  • 18