Python 过滤字典后打印出字典值

Python 过滤字典后打印出字典值,python,Python,我需要能够打印出符合thresh hold标准的正确Zipcode,我可以对它们进行过滤并对其进行操作,但最后一步是打印哪些Zipcode位于距离中心50英里的范围内。 这是我的密码 import sys import csv import math dicts = {} origin =[] #methods to convert to radians def getLatRad(latitude): return float(latitude) * (math.pi/180.0)

我需要能够打印出符合thresh hold标准的正确Zipcode,我可以对它们进行过滤并对其进行操作,但最后一步是打印哪些Zipcode位于距离中心50英里的范围内。 这是我的密码

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()
导入系统 导入csv 输入数学 dicts={} 来源=[] #方法转换为弧度 def getLatRad(纬度): 返回浮动(纬度)*(数学pi/180.0) def getLongRad(经度): 返回浮点(经度)*(数学pi/180.0) #方法来查找哪些zipcodes在thresh内 def getnearbylist(中心、阈值、ziplist): 尝试: f=打开(“zips.csv”) csvParser=csv.reader(f) 对于csvParser中的行: zipcode=行[0]。条带() 纬度=行[2]。替换(“,”).strip() 经度=行[3]。替换(“,”).strip() dicts[zipcode]={'zipcode':zipcode,'latitude':latitude,'longitude':longitude} 如果以dicts为中心: 原点=笔录[中心] longRad2=getLongRad(原点['经度]) latRad2=getLatRad(原点['纬度]) matched={match:dicts[match]用于ziplist中的match,如果dicts中的match} 对于匹配的x: longRad1=getLongRad(匹配的[x][“经度]) latRad1=getLatRad(匹配的[x]['纬度]) dlon=longRad2-longRad1 dlat=latRad2-latRad1 a=数学sin(dlat/2)**2+数学cos(latRad1)*数学cos(latRad2)*数学sin(dlon/2)**2 c=2*math.asin(math.sqrt(a)) m=3960*c
如果m您需要捕获附近发现的拉链

#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        
#在新的dict中捕捉附近的拉链
近拉链={}
对于匹配的x:
longRad1=getLongRad(匹配的[x][“经度])
latRad1=getLatRad(匹配的[x]['纬度])
dlon=longRad2-longRad1
dlat=latRad2-latRad1
a=数学sin(dlat/2)**2+数学cos(latRad1)*数学cos(latRad2)*数学sin(dlon/2)**2
c=2*math.asin(math.sqrt(a))
m=3960*c

如果m您需要捕获附近发现的拉链

#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        
#在新的dict中捕捉附近的拉链
近拉链={}
对于匹配的x:
longRad1=getLongRad(匹配的[x][“经度])
latRad1=getLatRad(匹配的[x]['纬度])
dlon=longRad2-longRad1
dlat=latRad2-latRad1
a=数学sin(dlat/2)**2+数学cos(latRad1)*数学cos(latRad2)*数学sin(dlon/2)**2
c=2*math.asin(math.sqrt(a))
m=3960*c

如果m您想只打印zipcodes?满足标准的zipcodes?您想只打印zipcodes?满足标准的zipcodes这只返回其中一个值,对于给定的zipcodes,它应该返回其中的2个值。我的示例将返回
匹配
m
中的任何值。因此,要么拉链不在
匹配的
,要么
m>=thresh
。您确定您的数学和阈值实现正确吗?我已经在阈值块中添加了print语句,以显示通过计算的值。这只返回其中一个值,对于给定的zipcodes,它应该返回其中的2个值。我的示例将返回
match
m
中的任何值。因此,要么拉链不在
匹配的
,要么
m>=thresh
。您确定您的数学和阈值实现正确吗?我在阈值块中添加了print语句,以显示通过计算的值。