Python - 传递函数参数
我正在努力弄清楚如何从函数传递参数,以便我可以在另一个函数中填充列表 - 我的代码是:
infinity = 1000000
invalid_node = -1
startNode = 0
#Values to assign to each node
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
#read in all network nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
print theNetwork
return theNetwork
#for each node assign default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print "The previous node is " ,nodeTable[index].previous
print "The distance from source is " ,nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
nearestNeighbour = []
nodeIndex = 0
for node in nodeTable:
if node != 0 and currentNode.visited == false:
nearestNeighbour.append(nodeIndex)
nodeIndex +=1
return nearestNeighbour
currentNode = startNode
if __name__ == "__main__":
nodeTable = populateNodeTable()
theNetwork = network()
nearestNeighbour(currentNode, theNetwork)
所以,我试图用最近的节点列表填充我的nearestNeighbour函数中的nearestNeighbour列表到其他节点。现在,所有其他函数都可以正常工作,所有参数传递都可以正常运行。 但是,我的nearestNeighbour函数会抛出此错误消息:
如果节点!= 0 并且 theNetwork[当前节点].visited == false:属性错误:“列表”对象 没有属性“访问过”
(对布局表示歉意,还没有完全理解代码引号的使用)
I am struggling on how to work out how I pass arguments from a function so that I can populate a list in another function - my code is:
infinity = 1000000
invalid_node = -1
startNode = 0
#Values to assign to each node
class Node:
distFromSource = infinity
previous = invalid_node
visited = False
#read in all network nodes
def network():
f = open ('network.txt', 'r')
theNetwork = [[int(node) for node in line.split(',')] for line in f.readlines()]
print theNetwork
return theNetwork
#for each node assign default values
def populateNodeTable():
nodeTable = []
index = 0
f = open('network.txt', 'r')
for line in f:
node = map(int, line.split(','))
nodeTable.append(Node())
print "The previous node is " ,nodeTable[index].previous
print "The distance from source is " ,nodeTable[index].distFromSource
index +=1
nodeTable[startNode].distFromSource = 0
return nodeTable
#find the nearest neighbour to a particular node
def nearestNeighbour(currentNode, theNetwork):
nearestNeighbour = []
nodeIndex = 0
for node in nodeTable:
if node != 0 and currentNode.visited == false:
nearestNeighbour.append(nodeIndex)
nodeIndex +=1
return nearestNeighbour
currentNode = startNode
if __name__ == "__main__":
nodeTable = populateNodeTable()
theNetwork = network()
nearestNeighbour(currentNode, theNetwork)
So, I am trying to fill the nearestNeighbour list in my nearestNeighbour function with a list of nodes nearest to the other nodes. Now, the all the other functions work correctly, with all argument passing functioning as it should.
However, my nearestNeighbour function throws up this error message:
if node != 0 and
theNetwork[currentNode].visited ==
false: AttributeError: 'list' object
has no attribute 'visited'
(Apologies for the layout, haven't quite fathomed the use of the code quotes yet)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为
theNetwork[currentNode]
返回一个列表。换句话说:theNetwork
是一个列表的列表。这是完成的行:
That's because
theNetwork[currentNode]
returns a list. In other words:theNetwork
is a list of lists.This is the line where it is done:
theNetwork
是一个列表列表。列表 (theNetwork[currentNode]
) 没有visited
属性。也许你的意图是这样的:
theNetwork
is a list of lists. A list (theNetwork[currentNode]
) doesn't have avisited
attribute.Perhaps you intended something like: