2017-07-14 7 views
0

Frage: Beim Ausführen des unten stehenden Codes wird ein Fehler angezeigt. Ich bin neu und weiß nicht, wie ich das Problem beheben kann. Creaefunktion, um jeden Koordinatenpunkt seinem Bezirk zuzuweisen.AttributeError: 'dict' Objekt hat kein Attribut '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

Sie versuchen, eine Python 2-Methode auf Python 3 zu verwenden. – user2357112

+0

verwenden Sie stattdessen 'boros.items()' – Sriram

+0

Danke Sriram. Es funktionierte! – Victor

Antwort

0

In Python 3 wurde dict.iteritems-dict.items umbenannt. Sie sollten diese Umbenennung auch in Ihrem Code vornehmen. In Python 2 funktioniert dict.items auch, obwohl dies eine Liste von Items zurückgibt, während dict.iteritems in Python 2 (und dict.items in Python 3) einen Generator zurückgibt, der ein Low-Memory-Looping über die Items ermöglicht.

Verwandte Themen