问题:错误:“’dict’对象没有属性’iteritems’”

我正在尝试使用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'

我正在使用Python 3.4,并通过pip install安装了NetworkX。

在发生此错误之前,它已经给我另一个提示“ xrange不存在”或类似名称,因此我进行了查找,然后将其更改xrangerangenx_shp.py文件,似乎可以解决该问题。

根据我的阅读,它可能与Python版本(Python2 vs Python3)有关。

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:

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.

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.

From what I’ve read it could be related to the Python version (Python2 vs Python3).


回答 0

正如您在python3中一样,请使用dict.items()代替dict.iteritems()

iteritems() 已在python3中删除,因此您无法再使用此方法。

看一下Python 3.0 Wiki的“ 内置更改”部分,其中指出:

删除dict.iteritems()dict.iterkeys()dict.itervalues()

相反:使用dict.items()dict.keys()dict.values() 分别。

As you are in python3 , use dict.items() instead of dict.iteritems()

iteritems() was removed in python3, so you can’t use this method anymore.

Take a look at Python 3.0 Wiki Built-in Changes section, where it is stated:

Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues().

Instead: use dict.items(), dict.keys(), and dict.values() respectively.


回答 1

Python2中,我们有.items().iteritems()在字典中。dict.items()返回字典中的元组列表[(k1,v1),(k2,v2),...]。它复制了字典中的所有元组并创建了新列表。如果字典很大,则对内存的影响很大。

因此,他们dict.iteritems()在更高版本的Python2中创建了代码。此返回的迭代器对象。未复制整个词典,因此内存消耗较少。使用人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

输出:

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

Python3,他们想使之更有效率,所以感动dictionary.iteritems()dict.items(),并删除.iteritems(),因为它不再需要。

您已经使用过dict.iteritems()Python3所以失败了。尝试使用dict.items()具有与相同功能dict.iteritems()Python2。这是从Python2到的一点点迁移问题Python3

In Python2, we had .items() and .iteritems() in dictionaries. dict.items() returned list of tuples in dictionary [(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. 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.

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.

You have used dict.iteritems() in Python3 so it has failed. Try using dict.items() which has the same functionality as dict.iteritems() of Python2. This is a tiny bit migration issue from Python2 to Python3.


回答 2

我有一个类似的问题(使用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

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.

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

回答 3

在Python2 中,该功能dictionary.iteritems()dictionary.items()Python3中的效率更高,该功能dictionary.iteritems()已迁移到dictionary.items()iteritems()已删除。因此,您将收到此错误。

dict.items()在Python3中使用,与Python2相同dict.iteritems()

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. So you are getting this error.

Use dict.items() in Python3 which is same as dict.iteritems() of Python2.


回答 4

这样做的目的.iteritems()是通过在循环时一次产生一个结果来使用较少的存储空间。我不确定为什么Python 3版本不支持,iteritems()尽管事实证明它比.items()

如果要包含同时支持PY版本2和3的代码,

try:
    iteritems
except NameError:
    iteritems = items

如果您在其他系统上部署项目并且不确定PY版本,这将有所帮助。

The purpose of .iteritems() was to use less memory space by yielding one result at a time while looping. I am not sure why Python 3 version does not support iteritems()though it’s been proved to be efficient than .items()

If you want to include a code that supports both the PY version 2 and 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.


回答 5

正如RafaelC回答的那样,Python 3重命名了dict.iteritems-> dict.items。尝试使用其他软件包版本。这将列出可用的软件包:

python -m pip install yourOwnPackageHere==

然后重新运行您要在==之后尝试安装/切换版本的版本

As answered by RafaelC, Python 3 renamed dict.iteritems -> dict.items. Try a different package version. This will list available packages:

python -m pip install yourOwnPackageHere==

Then rerun with the version you will try after == to install/switch version


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。