Python 错误:“'dict' 对象没有属性 'iteritems'”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30418481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: " 'dict' object has no attribute 'iteritems' "
提问by friveraa
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.
在这个错误之前它已经给了我另一个说“xrange不存在”或类似的东西,所以我查了一下,只是在 nx_shp.py 文件中更改xrange
为range
,这似乎解决了它。
From what I've read it could be related to the Python version (Python2 vs Python3).
从我读过的内容来看,它可能与 Python 版本(Python2 与 Python3)有关。
采纳答案by rafaelc
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 Changessection, where it is stated:
看看 Python 3.0 Wiki Built-in Changes部分,其中说明:
Removed
dict.iteritems()
,dict.iterkeys()
, anddict.itervalues()
.Instead: use
dict.items()
,dict.keys()
, anddict.values()
respectively.
删除
dict.iteritems()
,dict.iterkeys()
和dict.itervalues()
。相反:使用
dict.items()
,dict.keys()
和dict.values()
分别。
回答by Big Daddy
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
回答by Prabhu
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.
在Python2 中,我们在字典中有.items()
和.iteritems()
。dict.items()
返回字典中的元组列表[(k1,v1),(k2,v2),...]
。它复制字典中的所有元组并创建新列表。如果字典很大,则内存影响很大。
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.
所以他们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
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. 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
.
你已经使用dict.iteritems()
了Python3
所以它失败了。尝试使用dict.items()
与 具有相同功能dict.iteritems()
的Python2
。这是一个从Python2
到的微小迁移问题Python3
。
回答by Prabhu
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.
在 Python2 中,dictionary.iteritems()
比在 Python3中更高效dictionary.items()
,dictionary.iteritems()
已迁移dictionary.items()
并iteritems()
删除了 的功能。所以你会收到这个错误。
Use dict.items()
in Python3 which is same as dict.iteritems()
of Python2.
使用dict.items()
在Python3是相同dict.iteritems()
Python2的。
回答by DivyaMaheswaran
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()
的目的.iteritems()
是通过在循环时一次产生一个结果来使用更少的内存空间。我不确定为什么 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 版本,这会有所帮助。
回答by Punnerud
As answered by RafaelC, Python 3 renamed dict.iteritems -> dict.items. Try a different package version. This will list available packages:
正如 RafaelC 所回答的,Python 3 重命名了 dict.iteritems -> dict.items。尝试不同的软件包版本。这将列出可用的包:
python -m pip install yourOwnPackageHere==
Then rerun with the version you will try after == to install/switch version
然后使用您将在 == 之后尝试安装/切换版本的版本重新运行