This article brings you relevant knowledge about python, which mainly introduces related issues about containers, iterable objects, iterators and generators. Let’s take a look at them together. I hope everyone has to help.
Recommended learning: python video tutorial
When you first started learning Python, did you often hear the big guys We are talking about containers, iterable objects, iterators, generators, lists/sets/dictionary comprehensions and many other concepts. In fact, this is not because the big guys just use professional terminology to pretend to be B, but these things need to be Understand, just knowing the basics of strings, lists, etc. is not enough, especially in terms of Python's data structures.
Today I will tell you about the difficult-to-understand concepts of containers, iterable objects, iterators and generators in Python, so as to take your Python foundation to the next level!
In Python, a container is a data structure that organizes multiple elements together. The elements in the container can be iteratively obtained one by one. To put it bluntly, its function is just like its name: used to store things (data).
Container actually does not exist. It is not a data type, but a man-made concept. It is just a concept word created to facilitate learning. It can be used with the membership operator (in or not in) to determine whether the object is in the container.
Of course, it wasn’t created by me. I’m not that capable. It was created by the official government. You don’t have to worry. I’m teaching you some weird terms. If you say it, others will listen. I don’t understand…that’s how it’s called in python. Common container types include list (list), tuple (tuple), string (str), dictionary (dict) and set (set) .
Since the data in the container can be obtained iteratively, then we have to learn a new concept: iterable objects.
In python, an iterable object does not refer to a specific data type, it refers to a container object that stores elements.
In other words, if there is no data stored in the container, then it is not an iterable object. Not all containers are iterable objects. Containers include but are not limited to iterable objects.
Pay attention to two points:
1.很多容器都是可迭代对象(容器包含了可迭代对象)。 2.一个可迭代对象是不能独立的进行迭代的,迭代是通过for来完成的,凡是可迭代对象都可以直接使用for循环进行访问。
You should be familiar with the for loop, right? Have you ever wondered how the for loop is implemented? For example, in this for loop example, why can it output every element in the list? How is it implemented internally?
In fact, the for loop does two things:
1.使用 __iter__() 返回1个迭代器,迭代器在下面会讲,这里先知道有这么个东西。 2.使用 __next__() 获取迭代器中的每一个元素。
Then we don’t need a for loop to output each element in the list ,
l = [1,2,3,4]# for i in l:# print(i)ite =l.__iter__() #接收一下ietr()干了什么print(ite) #打印print(ite.__next__()) #for循环干第2件事情的时候做的第1步print(ite.__next__()) #for循环干第2件事情的时候做的第2步print(ite.__next__()) #for循环干第2件事情的时候做的第3步print(ite.__next__()) #for循环干第2件事情的时候做的第4步
Output result:
It can be seen that if we remove the line of code that prints ite, the execution effect will be the same as in the for loop output list Each element is the same. The for loop limits the range to 4 times. In fact, it executes __iter__() once and __next__() 4 times. In other words, the essence of the for loop accessing the iteration object is to do this. realized.
Moreover, the two things that the for loop essentially does are indispensable. That is to say, if __iter__() does not return the iterator first, __next()__ cannot obtain the element, exactly. This illustrates the second point of the two points mentioned earlier: an iterable object cannot be iterated independently.
There are two built-in functions that have the same principle and essence. Generally speaking, it is more convenient to use built-in functions. At least you don’t have to write so many underscores:
内置函数 iter() 的本质是 __inter__() ,也是返回一个迭代器。 内置函数 next() 的本质是 __next__(),也是有了迭代器之后获取元素。
It can be seen that the results are exactly the same. Since we talked about iterators, let’s take a look at what an iterator is.
We can probably see it through the for loop example above,
只要是实现了__iter__()和__next__()的对象,就是迭代器,迭代器是一个可迭代对象。 总之,迭代器是有__iter__()生成,可以通过__next__()进行调用。
In this case, we talked about range( when we were learning the basics of Python ) is an iterable object, then it can also generate an iterator through __iter__().
I mentioned the sequence in the special article [Assignment Statement], but I will talk about it again here, Sequence is also an abstract concept, which includes lists, tuples and strings. It does not exist in itself, and it is also a concept word created to facilitate learning.
可迭代对象包含序列,既然序列包含了列表、元组和字符串,前面我们的例子中也涉及到 了,所以说序列可以被iter()和next()使用。
序列可以分为有限序列和无限序列。有限序列就是有范围的,比如说range(10)就已经限定了范围,相反的,无限序列也就是没有限定范围的序列。
我们来生成一个无限序列,这里需要用到1个新模块itertools,itertools用于高效循环的迭代函数集合,它下面有一个方法count(),可生成迭代器且无范围,可以理解为无限迭代器。
通过这个例子我们可以看出来,只要执行一次,next()就会获取一次迭代器里面的内容并逐次获取,我这里只写了4个next(),你多写几次就会多输出几次。
像next()这种什么时候需要就什么时候调用的机制叫做懒加载机制,也叫懒汉式加载;
相反地就有饿汉式加载。比如for循环这种的,只要一执行就会把可迭代器里面的所有对象都获取。
列表推导式跟生成器有关,在讲生成器之前,需要先知道什么是列表推导式,列表推导式就是生成列表的一种方法,语法是这样的:
l = [i for i in 可迭代对象]
i表示要放进列表里的对象,for循环是一个式子。
比如我们用列表推导式来生成一个列表试试:
l = [i for i in range(5)]print(l)
运行结果:
[0, 1, 2, 3, 4]
运用列表推导式可以很方便地生成我们想要的列表。
同时它也有很多灵活的用法,比如在后面加上条件判断
l = [i for i in range(5) if 4<p>运行结果:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">[0, 1, 2, 3, 4]
if后面的条件判断为真,则可以正常生成列表,如果为假,则列表推导式是无效的,此时的l将是一个空列表。
还有其他灵活的用法,比如操作前面的i,比如让i的数值全都翻2倍:
我们把迭代对象换一下,换成字符串,也同样可以输出,只是*在字符串里面表示重复操作符,所以效果变成了这样:
不仅如此,前面的i*2我们还可以用函数来进行操作,比如:
总而言之,列表推导式就是用来快速和自定义生成列表的一种方法,很灵活。
那么有人可能会举一反三了,列表推导式都是用 [] 来进行操作的,那如果用()来操作行吗?它会不会生成一个元组?我们来看看:
[] 换成()之后,返回的是一个生成器generrator ,那么下面我们再来讲讲生成器:
生成器是真实存在于Python中的对象,与容器这种概念词是不同的,它是可以直接通过next()进行调用的。
第一种创建方法跟列表推导式是差不多的,就是 [] 换成了():
l = (i for i in 可迭代对象)
比如我们来生成一个生成器,看看能不能用next()直接调用:
l = (i for i in "abcd")print(next(l))
运行结果:
a
可以看出,生成器是可以直接调用的。那么既然生成器可以被next()调用,那么生成器就是一个特殊的迭代器,是一个可迭代对象。
除了用上面那种方法创建生成器,还可以用yield来创建,方法如下:
yield 关键字
比如说我们用一个函数中包含yield来创建生成器:
def fun(): a = 10 while 1: a += 1 yield a b = fun()print(b)
运行结果:
<generator></generator>
结果就是生成了一个生成器,而且此时的函数fun()就已经不再是一个函数了,它是一个生成器,只要函数中出现了yield,函数就变成了生成器。
为什么while循环没有一直执行?先不着急,我们输出看看:
def fun(): a = 10 while 1: a += 1 yield a b = fun()print(next(b))print(next(b))print(next(b))
运行结果:
111213
我调用了三次,所以它就运行了三次,while循环虽然存在,但是却不起作用,是因为前面我们提过的懒汉式加载。
什么时候需要了,什么时候用next()调用,就是懒汉式加载,不像饿汉式加载那样,提前生成了所有对象,如果这里换成for循环来完成,比如:
def fun(): a = 10 while 1: a += 1 print(a)b = fun()
运行之后程序将会进入死循环,一直给a自加1,你可以试试看效果,这就是饿汉式加载提前生成了迭代器并调用了全部迭代器对象,饿汉式加载占用资源的放大镜。
今天讲的内容可能听起来比较枯燥,这也是没得办法的,有些东西第一次听可能有点”难以下咽“,见得多了之后就习惯了,你得强迫自己去试着接受和理解这些抽象的东西。
最后用一张图来总结一下它们的关系:
推荐学习:python教程
The above is the detailed content of Python detailed analysis of containers, iterable objects, iterators and generators. For more information, please follow other related articles on the PHP Chinese website!