# Python'a Map FunctionWatch

#1
Hello,

I have a list of that I need converting to integers, called listOfNeighbours, which outputs [2, 3, 4, 5]
Code:
```#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
listOfNeighbours = []
nodeIndex = 0
for networkNode in theNetwork[currentNode]:
if networkNode != 0 and nodeTable[nodeIndex].visited == False:
listOfNeighbours.append(networkNode)
nodeIndex +=1
print "The nearest neighbours are", listOfNeighbours
##     #print node.distFromSource, node.previous, node.visited
##
return listOfNeighbours```
I've looked at the documentation but can't make head nor tail of it.
I have another map conversion in another function but I cannot see how I can apply that to the above function as it doesn't have a line:

Code:
`networkNode = map(int, line.split(','))`
0
quote
7 years ago
#2
You haven't really explained what you're trying to do or why you're using the map function. The map function takes a function identifier and a list and it returns a corresponding list where each element has had the named function applied to it.

Code:
```>>> import math
>>> map(math.sqrt, [9, 16, 25, 36])
>>> [3.0, 4.0, 5.0, 6.0]```
0
quote
#3
I'm writing Dijkstra's Algorithm, and I am at the point where I am calculating the tentative distance from the source node. As seen in my code, in my nearestNeighbour function, I have a list containing the nearest neighbours of a node, and to calculate the tentative distance, I need to convert that list into integers:

Code:
```infinity = 1000000
invalid_node = -1
startNode = 0

#Values to assign to each node
class Node:
def __init__(self):
self.distFromSource = infinity
self.previous = invalid_node
self.visited = False

#node = the distance values between nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(networkNode) for networkNode in line.split(',')] for line in f.readlines()]
#theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
print theNetwork

return theNetwork

#for each node assign default values
#populate table with default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
networkNode = map(int, line.split(','))
nodeTable.append(Node())

print "The previous node is " ,nodeTable[index].previous
print "The distance from source is " ,nodeTable[index].distFromSource
#print networkNode
index +=1
nodeTable[startNode].distFromSource = 0

return nodeTable

currentNode = startNode

#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
listOfNeighbours = []
nodeIndex = 0
for networkNode in theNetwork[currentNode]:
if networkNode != 0 and nodeTable[nodeIndex].visited == False:
listOfNeighbours.append(networkNode)
nodeIndex +=1
print "The nearest neighbours are", listOfNeighbours
##     #print node.distFromSource, node.previous, node.visited
##
return listOfNeighbours

def tentativeDistance (theNetwork, listOfNeighbours):
shortestPath = []
for nodeIndex in theNetwork:
currentDistance = listOfNeighbours[nodeIndex] + startNode
print currentDistance
##         if currentDistance[theNetwork][nodeIndex] < Node.distFromSource:
##            theNetwork[node].previous = nodeIndex
##            theNetwork[node].distFromSource = nodeIndex
##            theNetwork[node].visited = True;
##            shortestPath.append(indexNode)
##            nodeIndex +=1
##    print shortestPath

#def shortestPath(tentativeDistance)
#     shortestPath = []
#     f = open ('shortestPath.txt', 'w')
#     f.write(shortestPath)

#     f.close()

#currentNode = startNode

if __name__ == "__main__":
nodeTable = populateNodeTable()
#nodeTable = populateNodeTable(self)
theNetwork = network()
listOfNeighbours = nearestNeighbour(currentNode, theNetwork)
tentativeDistance(theNetwork, listOfNeighbours)```
When I execute my tentativeDistance function, I get this error:

Code:
```currentDistance = listOfNeighbours[nodeIndex] + startNode
TypeError: list indices must be integers, not list```
Even though I have used the map function before, I am not sure how I use it with this particular section of code
0
quote
7 years ago
#4
I already explained that error in the last thread you made. I'm still not sure what the map function has to do with anything.
0
quote
7 years ago
#5
x = map(f, [1, 2, 3]) is equivalent to x = [f(1), f(2), f(3)]
0
quote
X

new posts
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Do you like exams?

Yes (169)
18.51%
No (556)
60.9%
Not really bothered about them (188)
20.59%