简体   繁体   English

参考计算列表的部分(Python)

[英]Referring to parts of a list for a calculation (Python)

I have to calculate the distance between a set of points that are given and multiple points which come from a list. 我必须计算给定的一组点与来自列表的多个点之间的距离。

an example of a line from the list is; 列表中一行的示例是;

['14', '"Name of place"', '-31.000', '115.000']

As the calc distance function takes four parameters I put the two given points in then then long and lat values of the list. 由于calc距离函数有四个参数,我将两个给定点放在那里然后是列表的long和lat值。

My understanding was to do this I could simply refer to the list aka 'List' then which part of each line I want to access aka 2 and 3 我的理解是这样做我可以简单地参考列表又名'列表'然后我要访问的每行的哪一部分又称为2和3

        User_E = raw_input("First enter your longitude(easting) value")
        User_N = raw_input("Now enter your latitude(northing) value")
        Radius = raw_input("Now enter a search radius in kilometres")
        for lines in ListOfLandmarks:
            CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

when I run the program I receive the below error: 当我运行程序时,我收到以下错误:

TypeError: unsupported operand type(s) for -: 'str' and 'list'

Iv'e tried to use int and float to identify them as numbers but they produce the following: 我试图使用intfloat将它们标识为数字,但它们产生以下内容:

TypeError: int() argument must be a string or a number, not 'list'

TypeError: float() argument must be a string or a number

def CalculateDistance( latOne, lonOne, latTwo, lonTwo ):
DISTANCE_CONSTANT = 111120.0
coLat = math.fabs(lonOne - lonTwo)
alpha = 90 - latTwo
beta  = 90 - latOne

cosAlpha = math.cos(math.radians(alpha))
cosBeta  = math.cos(math.radians(beta))
sinAlpha = math.sin(math.radians(alpha))
sinBeta  = math.sin(math.radians(beta))
cosC     = math.cos(math.radians(coLat))

cos_of_angle_a = (cosAlpha * cosBeta)
cos_of_angle_b = (sinAlpha * sinBeta * cosC)
cos_of_angle_c = cos_of_angle_a + cos_of_angle_b
angle          = math.degrees(math.acos(cos_of_angle_c))
Distance       = angle * DISTANCE_CONSTANT
return Distance

Just wondering where I am going wrong, cheers! 只是想知道我哪里出错了,欢呼!

Skipping the conversion issues (as you store your coordinates as strings instead of floats) 跳过转换问题(将坐标存储为字符串而不是浮点数)

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

should be 应该

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, lines[2], lines[3] )

As you ask for distance to the particular landmark that you iterate through, ListOfLandmarks[2] is a second landmark (so a list which your interpreter does not know how to compare/use in the float context), while the first coordinate of the current landmark is lines[2] 当您询问与迭代的特定地标的距离时, ListOfLandmarks[2]第二个地标 (因此您的解释器不知道如何在float上下文中比较/使用的list ),而当前的第一个坐标地标是lines[2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM