Python 如何将要分配给节点的数字的字母更改为数字?
我想知道如何在我的代码中将A(大写和小写)放入节点0中,但我真的不知道如何。我知道有chr,但我以前从未使用过它,我试着阅读和使用它,但我就是无法理解它 这是我的密码:Python 如何将要分配给节点的数字的字母更改为数字?,python,python-2.7,chr,Python,Python 2.7,Chr,我想知道如何在我的代码中将A(大写和小写)放入节点0中,但我真的不知道如何。我知道有chr,但我以前从未使用过它,我试着阅读和使用它,但我就是无法理解它 这是我的密码: infinity = 1000000 invalid_node = -1 class Node: previous = invalid_node distFromSource = infinity visited = False def populateNetwork(fileName):
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
我的route.txt文件基本上是“A>G”、“A>G”或“A>G”。您可以使用文本
import string
print string.ascii_uppercase
# ABCDEFGHIJKLMNOPQRSTUVWXYZ
print string.ascii_lowercase
# abcdefghijklmnopqrstuvwxyz
print string.ascii_letters
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
char = string.ascii_uppercase[0] # A
char = string.ascii_uppercase[1] # B
char = string.ascii_uppercase[2] # C
char = string.ascii_lowercase[0] # a
char = string.ascii_lowercase[1] # b
char = string.ascii_lowercase[2] # c
或者您可以使用chr(number)
您可以使用ord(字母)
如果你不介意的话,还有一个问题,我编写了以下代码:def getRoute(getRouteFile):getRoutePath=[]routeFile=open(getRouteFile,“r”)对于routeFile中的路由:getRoutePath.append(map(ord,routes.split('>'))返回getRoutePath,这对我很有用。如何使用这两个ascii数字来计算哪个最小或更大?
import string
print string.ascii_uppercase
# ABCDEFGHIJKLMNOPQRSTUVWXYZ
print string.ascii_lowercase
# abcdefghijklmnopqrstuvwxyz
print string.ascii_letters
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
char = string.ascii_uppercase[0] # A
char = string.ascii_uppercase[1] # B
char = string.ascii_uppercase[2] # C
char = string.ascii_lowercase[0] # a
char = string.ascii_lowercase[1] # b
char = string.ascii_lowercase[2] # c
print chr(65 + 0) # A
print chr(65 + 1) # B
print chr(65 + 2) # C
print chr(97 + 0) # a
print chr(97 + 1) # b
print chr(97 + 2) # c
print ord('A') # 65
print ord('a') # 97