python - 属性错误 : 'dict' object has no attribute 'iteritems'

标签 python json iteritems

这个问题在这里已经有了答案:





Error: " 'dict' object has no attribute 'iteritems' "

(6 个回答)


2年前关闭。




问题:运行以下代码时出现错误。我是新手,不知道如何解决这个问题。 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'

最佳答案

在 Python 3 中,dict.iteritems更名为 dict.items .您也应该在代码中进行重命名。在 Python 2 中,dict.items也可以,虽然这会返回一个项目列表,而 dict.iteritems在 Python 2 中(和 dict.items 在 Python 3 中)返回一个生成器,启用对项目的低内存循环。

关于python - 属性错误 : 'dict' object has no attribute 'iteritems' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45112445/

相关文章:

python - 如何使用原始 SQL 查询实现搜索功能

javascript - 通过数组中每个 JSON 对象中的值从 JSON 数组中删除重复项

json - 如何将数据从 CosmosDB 中的文档传输到 Azure SQL 数据库?

python - 按第一个元素值对数组进行分组 Python

python - 如何从 Python 在 Gnome 中安全地清除两个剪贴板?

python 索引错误: list index out of range on arp_scan script

python - 如何在每个可能的数据包丢失网络时测试服务器行为

json - 在 JSON 中存储嵌套数据还是使用具有唯一名称的平面结构更好?

python - 断言错误 : Number of manager items must equal union of block items # manager items: 6004, # tot_items: 6005