Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the meaning of generator in python

Shulou Source: shulou.com Published: 2022-06-01 02:32:02 09月21日 Update

This article will explain in detail what the generator in python means. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Generator (Generator):

Through list generation, we can create a list directly. However, due to memory constraints, the list capacity is certainly limited. Moreover, creating a list of 1 million elements not only takes up a lot of storage space, but if we only need to access the first few elements, the space occupied by most of the later elements is wasted. In Python, this mechanism of calculating while looping is called a Generator.

Here is a simple generator:

> L = [x * x for x in range (10)] > L [0,1,4,9,16,25,36,49,64,81] > > g = (x * x for x in range (10)) > > g this article on "what is the meaning of generator in python" ends here. I hope the above content can be helpful to you so that you can learn more knowledge. Please share it for more people to see.

Tags: Generation element generator article meaning middle student more space good practical large memory content capacity article limited mechanism knowledge reference storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Linux MariaDB macOS