错误:“'dict'对象没有属性'iteritems'”

本文翻译自:Error: “ 'dict' object has no attribute 'iteritems' ”

I'm trying to use NetworkX to read a Shapefile and use the function write_shp() to generate the Shapefiles that will contain the nodes and edges, but when I try to run the code it gives me the following error: 我正在尝试使用NetworkX读取Shapefile并使用函数write_shp()生成将包含节点和边的Shapefile,但是当我尝试运行代码时,出现以下错误:

Traceback (most recent call last):   File
"C:/Users/Felipe/PycharmProjects/untitled/asdf.py", line 4, in
<module>
    nx.write_shp(redVial, "shapefiles")   File "C:\Python34\lib\site-packages\networkx\readwrite\nx_shp.py", line
192, in write_shp
    for key, data in e[2].iteritems(): AttributeError: 'dict' object has no attribute 'iteritems'

I'm using Python 3.4 and installed NetworkX via pip install. 我正在使用Python 3.4,并通过pip install安装了NetworkX。

Before this error it had already given me another one that said "xrange does not exist" or something like that, so I looked it up and just changed xrange to range in the nx_shp.py file, which seemed to solve it. 这个错误之前,它已经给了我另外一个说:“x范围不存在”或类似的东西,所以我看着它,只是改变xrangerange在nx_shp.py文件,这似乎解决它。

From what I've read it could be related to the Python version (Python2 vs Python3). 根据我的阅读,它可能与Python版本(Python2 vs Python3)有关。


#1楼

参考:https://stackoom.com/question/23dEf/错误-dict-对象没有属性-iteritems


#2楼

As you are in python3 , use dict.items() instead of dict.iteritems() 就像在python3中一样,使用dict.items()而不是dict.iteritems()

iteritems() was removed in python3, so you can't use this method anymore. iteritems()已在python3中删除,因此您无法再使用此方法。

Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated: 看一下Python 3.0 Wiki的“ 内置更改”部分,其中指出:

Removed dict.iteritems() , dict.iterkeys() , and dict.itervalues() . 删除了dict.iteritems()dict.iterkeys()dict.itervalues()

Instead: use dict.items() , dict.keys() , and dict.values() respectively. 相反:使用dict.items() dict.keys()dict.values()分别。


#3楼

I had a similar problem (using 3.5) and lost 1/2 a day to it but here is a something that works - I am retired and just learning Python so I can help my grandson (12) with it. 我有一个类似的问题(使用3.5),每天损失1/2,但这是可行的-我退休了,只是学习Python,所以我可以帮助我的孙子(12)。

mydict2={'Atlanta':78,'Macon':85,'Savannah':72}
maxval=(max(mydict2.values()))
print(maxval)
mykey=[key for key,value in mydict2.items()if value==maxval][0]
print(mykey)
YEILDS; 
85
Macon

#4楼

In Python2 , we had .items() and .iteritems() in dictionaries. Python2中 ,字典中有.items().iteritems() dict.items() returned list of tuples in dictionary [(k1,v1),(k2,v2),...] . dict.items()返回字典[(k1,v1),(k2,v2),...]中的元组列表。 It copied all tuples in dictionary and created new list. 它复制了字典中的所有元组并创建了新列表。 If dictionary is very big, there is very big memory impact. 如果字典很大,则对内存的影响很大。

So they created dict.iteritems() in later versions of Python2. 因此,他们在更高版本的Python2中创建了dict.iteritems() This returned iterator object. 此返回的迭代器对象。 Whole dictionary was not copied so there is lesser memory consumption. 未复制整个词典,因此内存消耗较少。 People using Python2 are taught to use dict.iteritems() instead of .items() for efficiency as explained in following code. 使用Python2被教导使用dict.iteritems()而不是.items()来提高效率,如以下代码中所述。

import timeit

d = {i:i*2 for i in xrange(10000000)}  
start = timeit.default_timer()
for key,value in d.items():
    tmp = key + value #do something like print
t1 = timeit.default_timer() - start

start = timeit.default_timer()
for key,value in d.iteritems():
    tmp = key + value
t2 = timeit.default_timer() - start

Output: 输出:

Time with d.items(): 9.04773592949
Time with d.iteritems(): 2.17707300186

In Python3 , they wanted to make it more efficient, so moved dictionary.iteritems() to dict.items() , and removed .iteritems() as it was no longer needed. Python3中 ,他们希望提高效率,因此将dictionary.iteritems()移至dict.items() ,并删除了.iteritems()因为不再需要它。

You have used dict.iteritems() in Python3 so it has failed. 您在Python3使用了dict.iteritems() ,因此失败了。 Try using dict.items() which has the same functionality as dict.iteritems() of Python2 . 尝试使用dict.items()其具有相同的功能dict.iteritems()Python2 This is a tiny bit migration issue from Python2 to Python3 . 这是从Python2Python3一点点迁移问题。


#5楼

In Python2, dictionary.iteritems() is more efficient than dictionary.items() so in Python3, the functionality of dictionary.iteritems() has been migrated to dictionary.items() and iteritems() is removed. 在Python2中, dictionary.iteritems()dictionary.items()效率更高,因此在Python3中, dictionary.iteritems()的功能已迁移到dictionary.items()并且iteritems()被删除。 So you are getting this error. 因此,您将收到此错误。

Use dict.items() in Python3 which is same as dict.iteritems() of Python2. 在Python3中使用dict.items()与Python2中的dict.iteritems()相同。


#6楼

The purpose of .iteritems() was to use less memory space by yielding one result at a time while looping. .iteritems()的目的是通过在循环时一次生成一个结果来使用更少的内存空间。 I am not sure why Python 3 version does not support iteritems() though it's been proved to be efficient than .items() 我不确定为什么Python 3版本不支持iteritems()尽管事实证明它比.items()更有效

If you want to include a code that supports both the PY version 2 and 3, 如果要包含同时支持PY版本2和3的代码,

try:
    iteritems
except NameError:
    iteritems = items

This can help if you deploy your project in some other system and you aren't sure about the PY version. 如果您在其他系统上部署项目并且不确定PY版本,这将有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值