简体   繁体   English

从2d列表中获取x,y并继续

[英]Get x,y from 2d list and pass on

I'm trying to take a 2d list of locations that incl lat/ longs and get only the coordinates from the list (in column 3 and 4) to send to another function so that distances can be calculated... but I'm totally stumped. 我正在尝试获取包含经度/纬度的位置的二维列表,并仅从列表中获取坐标(在第3列和第4列中)以发送给另一个函数,以便可以计算距离...但是我完全陷入困境 What I have is... 我所拥有的是...

EDITED from OP to show where I'm at... x1 = -18.00 #These values don't change y1 = 118.00 从OP编辑以显示我在哪里... x1 = -18.00#这些值不变y1 = 118.00
x2 = float(origList[3]) y2 = float(origList[4]) n = len(origList) appList = [] x2 = float(origList [3])y2 = float(origList [4])n = len(origList)appList = []

for i in range (n-1):
    appList.append(findDistance(x1, y1, x2, y2))
    print appList

But now I get...File "F:\\Storage\\t_2\\coord_find.py", line 27, in main,x2=math.fabs(origList[3]) TypeError: a float is required 但是现在我得到...文件“ F:\\ Storage \\ t_2 \\ coord_find.py”,第27行,位于main,x2 = math.fabs(origList [3])TypeError:需要浮点数

So the problem is in the conversion to float??? 所以问题出在向浮点数的转换中?

Then all I have so far for the findDistance function (I only just started it and just want to test if arguments are being passed correctly)... def findDistance(x1, y1, x2, y2): cosX = math.fabs(y1 - y2) a = 90 - x2 b = 90 - x1 然后,到目前为止,我对findDistance函数所拥有的一切(我只是刚刚启动了它,只想测试参数是否正确传递)... def findDistance(x1,y1,x2,y2):cosX = math.fabs(y1 -y2)a = 90-x2 b = 90-x1


Here's my updated code that is giving me this... 这是我给我的更新代码...

delx = math.fabs(y1 - y2) delx = math.fabs(y1-y2)

TypeError: unsupported operand type(s) for -: 'float' and 'list' TypeError:-:“ float”和“ list”的不受支持的操作数类型

I'm posting it up because I obviously didn't give a clear enough explanation and have made some changes since. 我之所以发布它,是因为我显然没有给出足够清晰的解释,并且此后进行了一些更改。 AS you can see, I want to get the x2,y2 from cols 3,4. 如您所见,我想从3,4列获取x2,y2。 Tried x2=float(origList[3]), y2=float(origList[4]) but that doesn't work either- I get "float()argument must be a string or a number". 尝试过x2 = float(origList [3]),y2 = float(origList [4]),但这都不起作用-我得到“ float()参数必须是字符串或数字”。 Do I need to split the list somehow before trying to extract the values as floats?? 在尝试将值提取为浮点数之前,是否需要以某种方式拆分列表?

import csv
import math

def fZone():
    origList = [['200','12_7','Cons_pl','-20.10','120.10','C_10_T2'],['....'...]]
    # origList has 30+ lines like this
    x1 = -20.68     # Fixed point
    x2 = 117.19     # Fixed point
    n = len(origList)   # list length
    appList = []    # to hold returned 

    for i in range (n):
        x2= origList[3] # I wanna get the '-20.10' col each iteration
        y2= origList[4] # I wanna get the '120.10' col each iteration
        appList.append(findDist(x1, y1, x2, y2))
        print appList

def findDist(x1,y1,x2,y2):
    delx = math.fabs(y1 - y2)
        a = 90 - x2
        b = 90 - x1  # formula is not finished

Your origList[0:] evaluates to origList . 您的origList[0:]计算为origList Try origList[0] . 尝试origList[0]

range(0,n-1) only goes up to n-2, and you have no need to specify the starting value of 0 since that's the default anyway. range(0,n-1)仅上升到n-2,并且您无需指定起始值0,因为无论如何这都是默认值。 Use range(n) . 使用range(n)

You say the coordinates are in columns 3 and 4. It looks like you're somewhat new to indexing in a programming language, so I hope I don't offend with a gratuitous reminder that you might be looking for list elements 2 and 3, if you mean that these are the 3rd and 4th columns. 您说坐标在第3列和第4列中。您似乎对使用编程语言建立索引有些陌生,所以我希望我不要冒昧地提醒您可能正在寻找列表元素2和3,如果您的意思是这些是第三和第四列。

Also, the print should be outside the loop. 同样, print应该在循环之外。

Apparently your list is containing pairs of strings, not of numbers. 显然,您的列表包含成对的字符串,而不是数字。

In Python this conversion is never automatic so you will probably have to change the code to: 在Python中,这种转换永远不会自动进行,因此您可能必须将代码更改为:

findDistance(x1, y1, float(origList[i][3]), float(origList[i][4]))

An error like 像这样的错误

lat2= float(stnList[3])
TypeError: float() argument must be a string or a number

clearly says what was passed to float was neither a number nor a string. 清楚地说,传递给float的东西既不是数字也不是字符串。

Just add a print repr(stnList[3]) right before the offending statement and check out what was passed instead. 只需在有问题的语句之前添加一个print repr(stnList[3])然后查看通过了什么。

You can also the python debugger to find what the problem is, check for example this short video of a pdb session example 您还可以通过python调试器来查找问题所在,例如查看以下有关pdb会话示例的简短视频

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

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