2

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

3
  • Your question is not very clear. Do you want to calculate the haversine of every possible pair of zipcodes? Commented Jan 24, 2013 at 23:38
  • Yes. My haversine function works as intended but I cant figure out how to do it for every possible pair. Commented Jan 24, 2013 at 23:40
  • Ok then, you've got your answer. Note that if you search for exactly what you want (python get all list combinations) you'll find that this has already been answered several times Commented Jan 25, 2013 at 0:01

4 Answers 4

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

Try using itertools, the combinations function may be what you want.

Comments

0

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

Comments

0

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 (i.e. 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)

1 Comment

You need to exclude all pairs that have the same zipcode twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.