Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:49:25+00:00 2026-05-30T18:49:25+00:00

I need to be able to print out the correct zipcodes that meet the

  • 0

I need to be able to print out the correct zipcodes that meet the thresh hold criteria, i can filter through them and operate on them, but the last step is to print which zipcodes are within 50miles of the center one.
here is my code

import sys
import csv
import math


dicts = {}
origin =[]
#methods to convert to radians
def getLatRad(latitude):
    return float(latitude) * (math.pi/180.0)
def getLongRad(longitude):
    return float(longitude) * (math.pi/180.0)
#method to find which zipcodes are within thresh
def getnearbylist(center, thresh, ziplist):
    try:
        f = open("zips.csv")
        csvParser = csv.reader(f)
        for row in csvParser:
            zipcode= row[0].strip()
            latitude= row[2].replace('"', '').strip()
            longitude=row[3].replace('"', '').strip()
            dicts[zipcode] = {'zipcode':zipcode,'latitude': latitude, 'longitude':longitude}
        if center in dicts:
            origin=dicts[center]
            longRad2= getLongRad(origin['longitude'])
            latRad2= getLatRad(origin['latitude'])
        matched = {match: dicts[match] for match in ziplist if match in dicts}
        for x in matched:
            longRad1= getLongRad(matched[x]['longitude'])
            latRad1= getLatRad(matched[x]['latitude'])
            dlon = longRad2 - longRad1 
            dlat = latRad2 - latRad1
            a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
            c = 2 * math.asin(math.sqrt(a)) 
            m = 3960 * c
            if m <thresh: # cant figure out how to return zipcodes instead of m value
                print m

    except ValueError:
        pass
def main():
    center = '12601'  # Our center zipcode
    thresh = 50  # We are looking for zipcodes within 50 miles
    ziplist = ['12481', '10001', '12203', '10303', '12561'] # Our test list

    nearbylist = getnearbylist(center, thresh, ziplist) # Call the function
    print nearbylist

if __name__ == '__main__':
    main()

so instead of printing m, i want to return the zipcodes
thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:49:26+00:00Added an answer on May 30, 2026 at 6:49 pm

    You need to capture the zips found to be nearby.

    #capture the nearby zips in a new dict
    near_zips={}
    for x in matched: 
        longRad1= getLongRad(matched[x]['longitude']) 
        latRad1= getLatRad(matched[x]['latitude']) 
        dlon = longRad2 - longRad1  
        dlat = latRad2 - latRad1 
        a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2 
        c = 2 * math.asin(math.sqrt(a))  
        m = 3960 * c 
        if m <thresh: # cant figure out how to return zipcodes instead of m value 
            #add the nearby zipcodes to the dict
            print '%f < %f' % (m,thresh)
            print 'adding %s to near_zips' % (x,)
            near_zips[x] = matched[x]
    
    #return the nearby zips
    return near_zips        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a PHP script that will print out results from a
I need to be able to print reports, general stuff. But I need it
I have a timetable in memory, and need to be able to print it
I have a college assignment where I need to print out items sold by
I need to be able to print text into a form field from an
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to create basic MS Project items (tasks, projects, resources,
I need to be able to get at the full URL of the page
I need to be able to get a list of the groups a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.