たとえば、A (大文字と小文字の両方) をコードのノード 0 に入れる方法を知りたいのですが、方法がわかりません。chr があることはわかりましたが、これまで使用したことがなく、読んで実行しようとしましたが、頭がわかりません。
これが私のコードです:
infinity = 1000000
invalid_node = -1
class Node:
previous = invalid_node
distFromSource = infinity
visited = False
def populateNetwork(fileName):
network = []
networkFile = open(fileName, "r")
for line in networkFile:
network.append(map(int, line.strip().split(',')))
return network
def populateNodeTable(network, StartNode):
nodeTable = []
for node in network:
nodeTable.append(Node())
nodeTable[StartNode].distFromSource = 0
nodeTable[StartNode].visited = True
return nodeTable
def nearestNeighbour(network, currentNode):
neighbours = []
column = 0
for node in network[currentNode]:
if node !=0:
neighbours.append(column)
column +=1
return neighbours
def tentativeDistance(neighbours, network, nodeTable, currentNode):
for neighbour in neighbours:
if nodeTable[neighbour].visited == False:
tentative = nodeTable[currentNode].distFromSource + network[currentNode][neighbour]
if tentative < nodeTable[neighbour].distFromSource:
nodeTable[neighbour].distFromSource = tentative
nodeTable[neighbour].previous = currentNode
print tentative
##Testing this Function
def newNodeTable(nodeTable):
nextNode == invalid_node
currentIndex = 0
nextDestination == infinity
for node in nodeTable:
if node.visited == False and node.distanceFromSource < nextDestination:
nextNode = currentIndex
nextDestination = node.distanceFromSource
currentIndex+=1
return nextNode
##Testing this Function
def getRoute(routePath):
getRoutePath = []
getRouteFile = open(routePath, "r")
for line in getRouteFile:
getRoutePath.append(int,line.split(">"))
return getRoutePath
network = populateNetwork('network.txt')
nodeTable = populateNodeTable(network, 1)
neighbours=nearestNeighbour(network, 1)
tentativeDistance(neighbours, network, nodeTable, 1)
print
print "Current nodes and visited"
for node in nodeTable:
print node.previous, node.distFromSource, node.visited
##for line in network:
## print line
print
print "Visual representation of the network array"
for index, val in enumerate(network):
print index, val
これが私のnetwork.txtファイルです:
0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,0,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0
私の route.txt ファイルは、基本的に "A>G" 、 "a>G" 、または "a>g" のいずれかになります。