我在通过 while 循环传递值时遇到问题。我在下面编写了一些伪代码,但我仍然不确定如何实现结果,但我在下面附上了我的代码,以提供帮助(如果可以的话)。通过 while 循环传递值的错误部分
首先,我的双重值列表如下。其中指的是名称、东距、北距
Stationlist = [['perth.csv','476050','7709929'],['sydney.csv','473791','7707713'],['melbourne.csv','46576','7691097']]
这是我正在使用的代码:
Import math
global Eastingbase
global Northingbase
Eastingbase=476050
Northingbase= 7709929
Def calculateDistance (northingOne, eastingOne, northingTwo, eastingTwo):
Base =100000
deltaEasting = eastingTwo -eastingOne
deltaNorthing = northingTwo -northingOne
Distance = (deltaEasting**2 + deltaNorthing**2) **0.5
If Distance < Base:
Return Distance
Def Radius():
1000
L=0
while L <= Len(Stationlist):
if calculateDistance(Eastingbase, Northingbase, Stationlist(row L, column 2),Stationlist(row L, column 3)) < Radius:
Relevantfilename = StationList (row L, column 1)
print Relevantfilename
L = +1
我的错误是我不确定如何将站列表中的值传递到 while 循环中,然后继续循环。我尝试使用双重列表理解,即 [0][1] 来传递名称,但它不起作用。另外,将加 1 添加到 L 似乎不会继续循环。有没有一种方法可以将一行中的所有值传递到 while 循环中并进行测试。?即,将 Perth.csv 传递到 Stationlist(L 行,第 1 列),将 476050 传递到 Stationlist(L 行,第 2 列),将 7709929 传递到 Stationlist(L 行,第 3 列)
完成后,对墨尔本和悉尼数据重复
最佳答案
您的代码中有很多错误/误解:
You should仅对类使用大写名称。事实上,它甚至不适用于
Import
和If
等内容(因为它们是语句,需要拼写正确:p)要访问列表中的元素,您可以使用索引(而不是列表理解,如您所解释的那样(这实际上是完全不同的事情))。例如,
print stationlist[0][2]
访问列表中的第一项,然后访问子列表中的第三项(请记住索引从 0 开始)如果您想将数字加一,请执行
L += 1
(注意符号的顺序)。这与L = L + 1
相同
我认为您误解了函数(尤其是半径函数)。您所需要做的就是
radius = 1000
。不需要任何功能:)。其他一些语法/缩进错误。
此处不应使用 while 循环。 for 循环 更好:
for station in stationlist: # Notice the lowercase
if calculateDistance(eastingbase, northingbase, station[1], station[2]) < radius:
print station[0]
请注意我如何使用 Python 的索引从列表中获取元素。我们不需要包含该行,因为我们使用的是 for 循环,它会遍历列表中的每个元素。
关于python - 通过 while 循环传递值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17739101/