2

我在通过我的 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 列)

一旦完成,然后重复墨尔本和悉尼数据

4

2 回答 2

2

您的代码中有许多错误/误解:

  • 您应该只对类使用大写名称。事实上,它甚至不适用于ImportIf(因为它们是语句,需要正确拼写: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 循环,它遍历列表中的每个元素。

于 2013-07-19T06:12:40.500 回答
0

您应该使用L += 1来增加索引。但不建议在 Python 中使用。而且Radius = 1000也不需要定义一个函数。

for each in Stationlist:    
    if calculateDistance(Eastingbase, Northingbase, each[1], each[2]) < Radius: 
        Relevantfilename = each[0]
        print Relevantfilename

我不明白为什么你剧本中的关键词以大写字母开头。Python 区分大小写,所以它是错误的。并且global不需要 s。

于 2013-07-19T06:09:47.363 回答