簡體   English   中英

Python腳本可以在Linux上運行,但不能在Windows上運行,我真的很絕望

[英]Python script works on linux but not on windows, I'm really desperate

請幫助我,我真的很絕望,我不知道該怎么辦。

因此,我們在大學分配了作業,以便在python中編寫dijkstra的算法。

INVALID_NODE = -1           #Define the initial variables to -1 and a very high number.
INFINITY = 1000000

#A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6
#network[0][1] is the cell that contains edge value for going from A to B

class Node:
    previous = INVALID_NODE         #With each node created, define it's initial variables to -1 and a very high number. 
    distFromSource = INFINITY
    visited = False         #Every node except starting node should be univisited by default.

def populateNetwork(filename):
    network = []                #Create an empty array to store the network file.
    networkfile = open(filename, "r")   #Open the network file.
    for line in networkfile:        #For every line in network file, remove \n's and split the data on every comma, then add everything to the array.
        line = line.replace("\n","")
        line = line.split(',')
        line = map(int, line)
        network.append(line)
    return network


def populateNodeTable(network, startNode):  #Populate the nodeTable with node objects based on the network file.
    nodeTable = []

    for node in network:            
        nodeTable.append(Node())

    nodeTable[startNode].distFromSource = 0 #Initialize the startNode distance from source and mark it as visited. 
    nodeTable[startNode].visited = True
    return nodeTable


def findNeighbours(network, nodeTable, currentNode):    #Function to find neighbours of the currentNode.
    nearestNeighbour = []               #Empty array to store neighbour indexes.
    columnIndex = 0

    for entry in network[currentNode]:      #check each node if it has a link to the currentNode and if it's unvisited, if both are true, add to the array.
        if entry != 0 and nodeTable[columnIndex].visited == False:
            nearestNeighbour.append(columnIndex)
        columnIndex += 1       
    return nearestNeighbour

def calculateTentative(network, nodeTable, currentNode, nearestNeighbours):

#Calculate the distance from currentNode to each node in neighborous list
#Work out distance from source for each node
#If lower than current entry in nodeTable for that node, update

    for neighbour in nearestNeighbours:
        tentativeDistance = nodeTable[currentNode].distFromSource + network[currentNode][neighbour]
        if nodeTable[neighbour].distFromSource > tentativeDistance:
            nodeTable[neighbour].distFromSource = tentativeDistance
            nodeTable[neighbour].previous = currentNode
    return nodeTable

def findNextNode(nodeTable):            #Find the next node from the neighbouring nodes with the lowest distFromSource.

    currentDistance = INFINITY
    nodeIndex = 0
    currentNode = INVALID_NODE

    for node in nodeTable:          #If it's distFromSource is less than distFromSource of the currentNode, make it the currentNode. 
        if(node.distFromSource < currentDistance) and (node.visited == False):
            currentNode = nodeIndex
            currentDistance = node.distFromSource
        nodeIndex += 1 

    return currentNode

####################
#Pseudocode from the internet for reference

#function Dijkstra(network, start):
#   for each node v in network:
#       distFromSource[v]:=infinity         #initial distance from source to vertex v is set to infinite
#       previous[v]:=undefined (-1)         #previous node in optimal path from source
#                               
#   distFromSource[source]:= 0              #distance from source to source
#   Q:= the set of all nodes in Graph           #all nodes in the graph are unoptimized - thus are in Q 
#
#   while Q is not empty:                   #main loop
#       u:=node in Q with smallest dist[]       
#       remove u from Q
#       
#       for each neighbour v of u:          #where v has not yet been removed from Q
#           alt:=dist[u] + dist_between(u,v)
#           if alt < dist[v]
#               dist[v]:=alt            #relax(u,v)
#               previous[v]:=u
#   return previous[]
#

####################
#Files

network = populateNetwork("network.txt")    #Populate the network array with network defined in netwowrk.txt file.
routefile = "route.txt"             #Load the route information.
letters = ["A", "B", "C", "D", "E",
 "F", "G", "H", "I", "J", "K", "L",     #Array of letters for easy conversion between integers and letters.
 "M", "N", "O", "P", "Q", "R", "S",
 "T", "U", "V", "W", "X", "Y", "Z"]


options = { "A": 0,     "B": 1,     #Dictionary to convert the initial route file to integers for much easier manipulation
        "C": 2,     "D": 3,     #within the program.
        "E": 4,     "F": 5,
        "G": 6,     "H": 7,
        "I": 8,     "J": 9,
        "K": 10,    "L": 11,
        "M": 12,    "N": 13,
        "O": 14,    "P": 15,
        "Q": 16,    "R": 17,
        "S": 18,    "T": 19,
        "U": 20,    "V": 21,
        "W": 22,    "X": 23,
        "Y": 24,    "Z": 25,
}

####################
#Initial data and initialisation

print("Network file:")              #Print the entire network.txt file to the screen line by line.
itr = 0                     #Iterator for line labeling.
for line in network:                
    print(letters[itr], ".", line)      #Prints letters associated with each node.
    itr = itr + 1

with open(routefile, "r") as rfile:     #Open route.txt and split it into two variables.
    routeData = rfile.read().split(">")

routeData[0] = routeData[0].rstrip("\n")    #Strip route data from additional \n's that can occur at the end of the file.
routeData[1] = routeData[1].rstrip("\n")

startNode = options[routeData[0]]       #Save both numbers as start and end nodes.
endNode = options[routeData[1]]


print("\nRoute:\n%s to %s\n" %(letters[startNode], letters[endNode]))   #Print start and end nodes to the user to make sure that
                        #the file was loaded correctly.         
                        #Populate node table with the network data and starting node.
currentNode = startNode             #Initialize currentNode to the start node.

print("Starting node:    %s\nDestination node: %s" %(network[startNode], network[endNode])) #Prints the starting and end nodes.

####################

nodeTable = populateNodeTable(network, currentNode) #Populates nodeTable with node objects from the network.

while currentNode is not endNode:           #While loop running as long as the currentNode isn't the destination.
    neighbours = findNeighbours(network, nodeTable, currentNode)    #Examine neighbours of the currentNode.
    nodeTable = calculateTentative(network, nodeTable, currentNode, neighbours) #Calculate tentative distances for the neighbours of the currentNode.
    nodeTable[currentNode].visited = True       #Mark current node as visited.
    currentNode = findNextNode(nodeTable)       #Jump to the next neighbour.
    #print "\nChecking node: "          #Print node the program is currently working on.
    #print letters[currentNode]         

####################

                            #New variable for the while loop.

routeList = []                      #Array to store final route data.

routeList.append(letters[currentNode])          #Add the final node to the array.

while currentNode is not startNode:         #Add all the nodes used in the final travel to the array using the nodetable[i].previous variable.
    routeList.append(letters[nodeTable[currentNode].previous]) 
    currentNode = nodeTable[currentNode].previous


print("\nShortest route between start and end nodes: ")
for entry in reversed(routeList):           #Print the array backwards for more intuitive output.
    print(entry,)

print("\n\nTotal distance traveled:",)          #Print the total distance traveled.
print(nodeTable[endNode].distFromSource)

####################
#Utility

print("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") #separator line for easier debugging

我是在筆記本電腦上運行的(它正在運行debian),它運行良好,腳本正在執行應有的操作,並為我提供了正確的輸出。

當我運行它時,它說:

TypeError: 'map' object is not subscriptable

但這是問題所在:我要在3個小時內將其到期,現在我要上傳它,並且我嘗試在台式機(Windows 10)上運行該文件以確保獲得正確的文件。

但是它正在起作用,它會吐出一個錯誤,我絕對不知道它是什么,因為我只是python的初學者,這是我第一次使用它。

請幫我! 我試圖弄清楚為什么它不能在Windows上運行,但超出了我的了解。

雖然它在Linux上可以100%正常工作...

請幫助我,即使我做了所有工作,我還是真的不想讓模塊失敗...

 - line = map(int, line)
 # in Python 3+ you will get the map object <map object at 0x.....>
 + line = list(map(int, line))

嘗試使用line.strip()而不是line = line.replace("\\n","") Windows行尾為'\\ r \\ n'...

Python 3, map()返回一個地圖對象,請參見:

x=[1,2,4,5,6,7,8,8,9]
y = map(str,x)
z=[]
z.append(y)
print(z) 
#[<map object at 0x7f1e65205ac8>]

在Python 2中,您將獲得相同的代碼:

#[['1', '2', '4', '5', '6', '7', '8', '8', '9']]

解決方案,在Python 3中將地圖對象投射回列表。 該解決方案也適用於Python 2,並且向后兼容:

x=[1,2,4,5,6,7,8,8,9]
y = map(str,x)
z=[]
z.append(list(y))
print(z) 
#[['1', '2', '4', '5', '6', '7', '8', '8', '9']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM