2017-07-14 63 views
0

問題:運行下面的代碼時出現錯誤。我是新手,不確定如何解決問題。 creae功能將每個座標點分配給它的區域。AttributeError:'dict'對象沒有屬性'iteritems'

def find_borough(lat,lon): 
     """ 
     return the borough of a location given its latitude and longitude 
     lat: float, latitude 
     lon: float, longitude 
     """ 
     boro = 0 # initialize borough as 0 
     for k,v in boros.iteritems(): # update boro to the right key corresponding to the parent polygon 
      if v['polygon'].contains(Point(lon,lat)): 
       boro = k 
       break # break the loop once the borough is found 
     return [boro] 

## Analyse the cluster now 
# create data frame of boroughs 
    df = data1[data1.Trip_duration>=1350] 
    orig_dest = [] 
    for v in df[['Pickup_latitude','Pickup_longitude','Dropoff_latitude','Dropoff_longitude']].values: 
     orig_dest.append((find_borough(v[0],v[1])[0],find_borough(v[2],v[3])[0])) 
    df2 = pd.DataFrame(orig_dest) 



     --------------------------------------------------------------------------- 
     AttributeError       Traceback (most recent call last) 
     <ipython-input-92-6a4861346be4> in <module>() 
      35 orig_dest = [] 
      36 for v in df[['Pickup_latitude','Pickup_longitude','Dropoff_latitude','Dropoff_longitude']].values: 
     ---> 37  orig_dest.append((find_borough(v[0],v[1])[0],find_borough(v[2],v[3])[0])) 
      38 df2 = pd.DataFrame(orig_dest) 
      39 

     <ipython-input-92-6a4861346be4> in find_borough(lat, lon) 
      24  """ 
      25  boro = 0 # initialize borough as 0 
     ---> 26  for k,v in boros.iteritems(): # update boro to the right key corresponding to the parent polygon 
      27   if v['polygon'].contains(Point(lon,lat)): 
      28    boro = k 

     AttributeError: 'dict' object has no attribute 'iteritems' 
+0

您試圖在Python 3上使用Python 2方法。 – user2357112

+0

使用'boros.items()'代替 – Sriram

+0

謝謝Sriram。有效! – Victor

回答

0

在Python 3中,dict.iteritems被重命名爲dict.items。你應該在你的代碼中重命名。在Python 2中,dict.items也可以工作,雖然這會返回一個項目列表,而Python 2中的dict.iteritems(和Python 3中的dict.items)會返回一個生成器,從而實現對項目的低內存循環。