总结
一.迭代器
-
什么是迭代器
- 迭代器是容器型数据类型
- 特点:无法通过打印迭代器查看元素、也无法统计元素个数;
- 需要使用元素的时候必须将元素从迭代器中取出,取出以后该元素就从迭代器中消失
-
创建迭代器 - 将其他序列转换成迭代器
i1 = iter('hello') i2 = iter([10, 20, 30, 40]) i3 = iter({ 'a': 10, 'b': 20}) i4 = iter({ 100, 200, 300}) print(i1) # <str_iterator object at 0x7fc25598fc40> print(i2) # <list_iterator object at 0x7fb21888fc70> print(i3) print(i4) # print(len(i1)) # 报错!TypeError: object of type 'str_iterator' has no len()
-
获取迭代器元素