Differences between list generators and generators in Python
List generative Syntax:
[X * x for x in range ()] // generate the list. Here is the brackets // result [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] (x * x for x in range () // generator, in parentheses // result <generator object <genexpr> at 0x7f0b072e6140>
The difference between the two is obvious:
One directly returns the expression result list, and the other is an object that contains computation references to the expression result and can be directly output through loops.
G = (x * x for x in range (0, 10) for n in g: print n
Result
0149162536496481
When the expression has a small number of results, it is better to use the list generator. Once the order of magnitude is too large, the list generator occupies a large amount of memory,
The generator does not immediately write the results into the memory, but stores a computing method. Through continuous acquisition, it can get the values at the corresponding locations, therefore, the occupied memory is only used to store computing objects.