简体   繁体   中英

Iterating over Python objects

I am attempting to write a program that calculates a certain formula based on zipcode, lat, and lng.

My initial idea was to create an object for each zip code.

class destination():
def __init__(self, zipcode, count):
    self.zipcode = zipcode
    self.count = count

def getCount(self):
    return self.count

def getZip(self):
    return self.zipcode

def getLatitude(self):
    return self.lat 

def getLongitude(self):
    return self.lng 

def __str__(self):
    return "%s at %s , %s" % (self.zipcode, self.lat, self.lng)

def getCoords(self):
    '''
    Must be called before getLatitude or get Longitude
    '''
    self.place, (self.lat, self.lng) = gn.geocode(str(self.zipcode))  
    self.city = self.place.split(",",1)
    self.name =  self.city[0]
    self.value = str(count)+","+self.name

    return self.value

That works fine as I can successfully iterate over a list and create the object and extract the necessary information out of i

zipList = ['54971','46383','90210']

for i in zipList:
    i = destination(i,count)
        count += 1

Will return

1,Ripon
-88.8359447
43.8422049
2,Valparaiso
-87.0611412
41.4730948
3,Beverly Hills
-118.4003563
34.0736204

What I cant seem to wrap my head around is how to set up the program so that it iterates through the list calling the haversine function with the correct information for each item.

def haversine(latStart,lonStart,latEnd,lonEnd):

Example: if my list is

zipList = ['54971','46383','90210']

Then it will do the calculation for 54971 to 46383, 54971 to 90210, and 46383 to 90210

尝试使用itertools,可能需要使用组合功能

Ask for all pairs of zipcodes from the list, and use them:

import itertools

for start, stop in itertools.combinations(zipList, 2):
    print start, stop
    # now pass start, stop to your function

You can create a list of destination objects and get the combinations of the created list and iterate the returned generator through the haversine function.

dests = []
for i in zipList:
    dests.append(destination(i,count))
    count += 1

dests_gen = itertools.combinations(dests, 2)
for dest_typle in dests_gen:
    pass

Short answer:

for a, b in ( (a, b) for a in zipList for b in zipList):
    print (a, b, distance (a, b) )

Some comments: You don't need to take manual control of "count" if you make it a class variable. You can use properties to geolocate your point on demand (ie when lat or lon are first accessed). You don't really need getter methods if the properties are public (Unless the API requires this). Maybe something like this.

#! /usr/bin/python3.2

def haversine (latStart,lonStart,latEnd,lonEnd): return 42

class Destination():
    count = 0

    def __init__(self, zipcode):
        self.zipcode = zipcode
        self.count = self.__class__.count
        self.__class__.count += 1
        self.__coords = None

    @property
    def latitude (self):
        if not self.__coords: self.__locate ()
        return self.__coords [0]

    @property
    def longitude (self):
        if not self.__coords: self.__locate ()
        return self.__coords [1]

    def __str__(self):
        return "%s at %s , %s" % (self.zipcode, self.latitude, self.longitude)

    def __locate (self):
        '''
        Will be called automatically before getLatitude or get Longitude
        '''
        self.place, self.__coords = gn.geocode (str (self.zipcode) )  
        self.city = self.place.split (",",1)
        self.name =  self.city [0]

    def distance (self, other):
        return haversine (self.latitude, self.longitude, other.latitude, other.longitude)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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