如何传递值并使用此功能

时间:2013-06-23 00:29:48

标签: 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,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

我遇到的问题是如何将Easting(左)和Northing(右)的这些值导入东向和北向列表。有人可以帮忙解决这个问题,因为我完全不确定如何让他们进入main2()中的东向和北向列表

我是否通过从东向和北向列表中调用东向和北向的值来正确实现该函数?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

注意:我假设getStationCoords()返回:

[['476050', '7709929'], ['473971', '7707713'], ['465676', '7691097'], ['515612', '7702192'], ['516655', '7704405'], ['519788', '7713255'], ['538466', '7683341']]

您可以使用zip()

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]

无论你在哪里实现,这完全取决于你,因为你是编写代码的人:p。