我是靠谱客的博主 俭朴黄豆,这篇文章主要介绍Python三大器——迭代器,现在分享给大家,希望可以做个参考。

1 写在最前

首先明确一点,在Python中:

  • 迭代器(Iterator)是一个可迭代(Iterable)对象
  • 可迭代对象不一定是迭代器

2 迭代器与可迭代对象

  • 迭代器:定义并实现了__iter__()__next__()方法的对象,可以使用var.__next__()next(var)访问下一个元素。
  • 可迭代对象:简而言之,就是可以用for ... in ...循环遍历的对象。

在Python中,列表,元组,字符串,字典,集合都是可迭代对象,但是它们都不是迭代器。 原因是,上述对象都内置了__iter__()方法,但是都未内置__next__()方法。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from typing import Iterable, Iterator print(isinstance([], Iterable)) # True print(isinstance((), Iterable)) # True print(isinstance("", Iterable)) # True print(isinstance(dict(), Iterable)) # True print(isinstance(set(), Iterable)) # True print(isinstance([], Iterator)) # False print(isinstance((), Iterator)) # False print(isinstance("", Iterator)) # False print(isinstance(dict(), Iterator)) # False print(isinstance(set(), Iterator)) # False

但是可以使用var__iter__()iter(var)来获取一个迭代器:

复制代码
1
2
3
4
5
6
7
8
9
10
from typing import Iterator nums = [i for i in range(10)] print(isinstance(nums, Iterator)) # False nums_iter1 = nums.__iter__() nums_iter2 = iter(nums) print(isinstance(nums_iter1, Iterator)) # True print(isinstance(nums_iter2, Iterator)) # True

3 itertools库——迭代器

3.1 排列组合迭代器

3.1.1 product( )——笛卡尔积

与嵌套的for循环类似:

复制代码
1
2
3
4
5
import itertools for i in itertools.product('ABC', '123'): print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
8
9
10
('A', '1') ('A', '2') ('A', '3') ('B', '1') ('B', '2') ('B', '3') ('C', '1') ('C', '2') ('C', '3')

3.1.2 permutations( )——排列

复制代码
1
2
3
for i in itertools.permutations('ABC', 2): # 2 是排列的长度 print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
('A', 'B') ('A', 'C') ('B', 'A') ('B', 'C') ('C', 'A') ('C', 'B')

3.1.3 combinations( )——组合

复制代码
1
2
3
for i in itertools.combinations('ABC', 2): # 2是组合的长度 print(i)

输出结果:

复制代码
1
2
3
4
('A', 'B') ('A', 'C') ('B', 'C')

3.1.4 combinations_with_replacement( )——元素可重复组合

复制代码
1
2
3
for i in itertools.combinations_with_replacement('ABC', 2): # 2是组合的长度 print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
('A', 'A') ('A', 'B') ('A', 'C') ('B', 'B') ('B', 'C') ('C', 'C')

3.2 无穷迭代器

3.2.1 count( )——计数

复制代码
1
2
my_count = itertools.count(start=100, step=2)

3.2.2 cycle( )——循环

复制代码
1
2
my_cycle = itertools.cycle("ABC")

3.2.3 repeat( )——重复

复制代码
1
2
my_repeat = itertools.repeat('A', 3)

3.3 拉链

3.3.1 zip( )——短拉链

长度不一时,执行到最短的对象处,就停止:

复制代码
1
2
3
4
5
6
7
8
9
from typing import Iterator my_zip = zip("ABC", "123456") print(isinstance(my_zip, Iterator)) for i in my_zip: print(i)

输出结果:

复制代码
1
2
3
4
5
True ('A', '1') ('B', '2') ('C', '3')

3.3.2 zip_longest( )——长拉链

长度不一时,执行到最长的对象处才停止,缺省元素用None或指定字符替代:

复制代码
1
2
3
4
5
6
7
8
9
10
from typing import Iterator my_zip_longest = itertools.zip_longest("ABC", "123456", fillvalue = "#") print(isinstance(my_zip_longest, Iterator)) for i in my_zip_longest: print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
8
True ('A', '1') ('B', '2') ('C', '3') ('#', '4') ('#', '5') ('#', '6')

3.4 枚举与锁链

3.4.1 enumerate( )——枚举

产出由两个元素组成的元组,结构是(index, item),其中indexstart开始,item从可迭代对象中取:

复制代码
1
2
3
for i in enumerate("ABCDEFG", start=1): print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
8
(1, 'A') (2, 'B') (3, 'C') (4, 'D') (5, 'E') (6, 'F') (7, 'G')

3.4.2 chain( )——锁链

把一组迭代对象串联起来,形成一个更大的迭代器:

复制代码
1
2
3
for i in itertools.chain('ABC', [1, 2, 3]): print(i)

输出结果:

复制代码
1
2
3
4
5
6
7
A B C 1 2 3

3.5 其他

其他使用方式参见Python官方文档:https://docs.python.org/3/library/itertools.html

最后

以上就是俭朴黄豆最近收集整理的关于Python三大器——迭代器的全部内容,更多相关Python三大器——迭代器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部