我在通过 while 循环传递值时遇到问题。我在下面用一些伪代码进行了编码,但我仍然不确定如何实现结果,但我已经在下面附上了我的代码以帮助它。通过 while 循环传递值的错误部分
首先,我的双重值(value)观列表如下。指名、东、北
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] 来传递名称,但它不起作用。另外向 L 添加 1 似乎不会继续循环。有没有办法将一行中的所有值传递到 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 循环,它遍历列表中的每个元素。
