8

问题:运行以下代码时出现错误。我是新手,不知道如何解决这个问题。creae 函数将每个坐标点分配给它的自治市镇。

    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'
4

1 回答 1

16

在 Python 3 中,dict.iteritems被重命名为dict.items. 您也应该在代码中进行此重命名。在 Python 2 中,dict.items它也可以工作,尽管这将返回一个项目列表,而dict.iteritems在 Python 2(和dict.itemsPython 3 中)返回一个生成器,从而实现对项目的低内存循环。

于 2017-07-14T22:37:10.327 回答