Python 如何传递值和使用此函数
嗨,我有以下代码Python 如何传递值和使用此函数,python,list,parsing,formula,Python,List,Parsing,Formula,嗨,我有以下代码 import csv import math EASTING_BASE = 500 NORTHING_BASE = 500 def main(): global NORTHING_BASE global EASTING_BASE for i, a in getStationCoords(): statione = int(i) stationn = int(a) print statione,sta
import csv
import math
EASTING_BASE = 500
NORTHING_BASE = 500
def main():
global NORTHING_BASE
global EASTING_BASE
for i, a in getStationCoords():
statione = int(i)
stationn = int(a)
print statione,stationn
stationEasting = int(getStationCoords()[0][0])
stationNorthing = int(getStationCoords()[0][1])
for i in range(0, len(eastingList)):
print "Co-ordinates (" + str(eastingList[i]) + "," + str(northingList[i]) + ")"
print calculateDistance(NORTHING_BASE, EASTING_BASE, northingList[i], eastingList[i])
print calculateBearing(NORTHING_BASE, EASTING_BASE, northingList[i], eastingList[i])
def getStationCoords():
listStation = []
a_reader = None
a_reader = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)
a_csv_reader.next()
for i in [row[-2:] for row in a_csv_reader]:
listStation.append(i)
a_reader.close()
count = 0
sum = 0.0
a_reader = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)
#for row in a_csv_reader:
# if count != 0 and row[0] != '':
# sum = sum + float(row[0])
# count = count + 1
#print 'Number of lines is:',count
#print 'Sum is:',sum
return listStation
def main2():
global NORTHING_BASE
global EASTING_BASE
eastingList = []
northingList = []
def calculateDistance(northingOne, eastingOne, northingTwo, eastingTwo):
# Determine differences in eastings and northings
deltaEasting = eastingTwo - eastingOne
deltaNorthing = northingTwo - northingOne
# We don't need to worry about +/- as using pythag below
distance = (deltaEasting **2 + deltaNorthing **2) **0.5
# Return the value for distance
return distance
def calculateBearing(northingOne, eastingOne, northingTwo, eastingTwo):
diffEasting = eastingTwo - eastingOne
diffNorthing = northingTwo - northingOne
# Work out if in QI/II or QIII/IV
if diffEasting >= 0:
# This is in QI/II
if diffNorthing >0:
# This is in QI
bearing = math.atan(diffEasting / diffNorthing)
else:
# This is in QII
bearing = math.pi - math.atan(diffEasting / abs(diffNorthing))
else:
# This is in QIII/IV
if diffNorthing >0:
# This is in QIV
bearing = 2 * math.pi - math.atan(abs(diffEasting) / diffNorthing)
else:
# This is in QIII
bearing = math.pi + math.atan(abs(diffEasting) / abs(diffNorthing))
# Return the value
return bearing
main2()
main()
好的,我知道我必须在东距和北距列表中放置值。在下面我的第一个函数main()生成以下内容
476050 7709929
473971 7707713
465676 7691097
515612 7702192
516655 7704405
519788 7713255
538466 7683341
我陷入困境的是如何将这些东距(左)和北距(右)值放入东距和北距列表中。有人能帮我一下吗?因为我完全不知道如何将他们列入main2()中的东距和北距列表
通过调用easting和northing列表中的easting和northing值,我是否正确地实现了该函数
非常感谢您的帮助 注意:我假设
getstationwords()
返回:
[['476050', '7709929'], ['473971', '7707713'], ['465676', '7691097'], ['515612', '7702192'], ['516655', '7704405'], ['519788', '7713255'], ['538466', '7683341']]
您可以使用:
无论在何处实现,这完全取决于您,因为您是编写代码的人:p.那么,请澄清一下,您希望左侧的所有值都在一个单独的列表中吗?右边所有的都在一个单独的列表中吗?@Haidro是的,就是这样。我想将左侧的所有值放入eastlist=[]中,将右侧的所有值放入northlist=[]中,这可能吗?/我在哪里实现这一点?我添加了一个答案Thank you@Haidro这很完美我想我可能需要更早地将值拆分,但如果列表已经存在,那么zip是一个更好的方法。热爱你的工作和你的工作。Thanksim试图通过从列表中调用来实现这一点,但是如果我使用,它对字符串和int不起作用-有办法解决吗?例如,我可以从列表中的字符串中删除它们吗?@KristoferWright是否希望它们作为整数而不是字符串?是的,它们可以是列表中的整数而不是字符串吗?这可能吗
eastlist, northlist = [map(int, i) for i in zip(*getStationCoords())]
print eastlist
# [476050, 473971, 465676, 515612, 516655, 519788, 538466]
print northlist
# [7709929, 7707713, 7691097, 7702192, 7704405, 7713255, 7683341]