some_dict.items() 是 Python 中的迭代器吗?
Is some_dict.items() an iterator in Python?
我对迭代器和可迭代对象之间的区别有点困惑。我读了很多书,得到了这么多:
Iterator:一个包含 __next__
的对象是 class。您可以在其上调用 next() 。所有迭代器都是可迭代的。
Iterable:在class中定义__iter__
或__getitem__
的对象。如果某物可以使用 iter() 构建迭代器,则它是可迭代的。并非所有可迭代对象都是迭代器。
some_dict.items()
是迭代器吗?我知道 some_dict.iteritems()
会在 Python2 对吧?
我只是在检查,因为我正在做的一门课程说它是,而且我很确定它只是一个可迭代的(不是迭代器)。
感谢您的帮助:)
你可以直接测试这个:
from collections import Iterator, Iterable
a = {}
print(isinstance(a, Iterator)) # -> False
print(isinstance(a, Iterable)) # -> True
print(isinstance(a.items(), Iterator)) # -> False
print(isinstance(a.items(), Iterable)) # -> True
不,不是。它是字典中项目的可迭代视图:
>>> next({}.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_items' object is not an iterator
>>>
它是 __iter__
方法 returns 一个专门的迭代器实例:
>>> iter({}.items())
<dict_itemiterator object at 0x10478c1d8>
>>>
自己查:
d = {'a': 1, 'b': 2}
it = d.items()
print(next(it))
这导致 TypeError: 'dict_items' object is not an iterator
。
另一方面,您始终可以迭代 d.items()
为:
d = {'a': 1, 'b': 2}
for k, v in d.items():
print(k, v)
或:
d = {'a': 1, 'b': 2}
it = iter(d.items())
print(next(it)) # ('a', 1)
print(next(it)) # ('b', 2)
dict.items
returns一个dict view, according to the docs:
In [5]: d = {1: 2}
In [6]: next(d.items())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-945b6258a834> in <module>()
----> 1 next(d.items())
TypeError: 'dict_items' object is not an iterator
In [7]: next(iter(d.items()))
Out[7]: (1, 2)
回答您的问题,dict.items
不是迭代器。它是一个可迭代对象,支持 len
、__contains__
并反映在原始字典中所做的更改:
In [14]: d = {1: 2, 3: 4}
In [15]: it = iter(d.items())
In [16]: next(it)
Out[16]: (1, 2)
In [17]: d[3] = 5
In [18]: next(it)
Out[18]: (3, 5)
我对迭代器和可迭代对象之间的区别有点困惑。我读了很多书,得到了这么多:
Iterator:一个包含 __next__
的对象是 class。您可以在其上调用 next() 。所有迭代器都是可迭代的。
Iterable:在class中定义__iter__
或__getitem__
的对象。如果某物可以使用 iter() 构建迭代器,则它是可迭代的。并非所有可迭代对象都是迭代器。
some_dict.items()
是迭代器吗?我知道 some_dict.iteritems()
会在 Python2 对吧?
我只是在检查,因为我正在做的一门课程说它是,而且我很确定它只是一个可迭代的(不是迭代器)。
感谢您的帮助:)
你可以直接测试这个:
from collections import Iterator, Iterable
a = {}
print(isinstance(a, Iterator)) # -> False
print(isinstance(a, Iterable)) # -> True
print(isinstance(a.items(), Iterator)) # -> False
print(isinstance(a.items(), Iterable)) # -> True
不,不是。它是字典中项目的可迭代视图:
>>> next({}.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_items' object is not an iterator
>>>
它是 __iter__
方法 returns 一个专门的迭代器实例:
>>> iter({}.items())
<dict_itemiterator object at 0x10478c1d8>
>>>
自己查:
d = {'a': 1, 'b': 2}
it = d.items()
print(next(it))
这导致 TypeError: 'dict_items' object is not an iterator
。
另一方面,您始终可以迭代 d.items()
为:
d = {'a': 1, 'b': 2}
for k, v in d.items():
print(k, v)
或:
d = {'a': 1, 'b': 2}
it = iter(d.items())
print(next(it)) # ('a', 1)
print(next(it)) # ('b', 2)
dict.items
returns一个dict view, according to the docs:
In [5]: d = {1: 2}
In [6]: next(d.items())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-945b6258a834> in <module>()
----> 1 next(d.items())
TypeError: 'dict_items' object is not an iterator
In [7]: next(iter(d.items()))
Out[7]: (1, 2)
回答您的问题,dict.items
不是迭代器。它是一个可迭代对象,支持 len
、__contains__
并反映在原始字典中所做的更改:
In [14]: d = {1: 2, 3: 4}
In [15]: it = iter(d.items())
In [16]: next(it)
Out[16]: (1, 2)
In [17]: d[3] = 5
In [18]: next(it)
Out[18]: (3, 5)