- 定义:通过算法来定义数组。
- 目的:可以减少数据存储。
示例代码:
import time
def _list(n):
l1 = [list(range(n)) for i in range(n)]
del l1
def _generator(n):
ge = (tuple(range(n)) for i in range(n))
del ge
start_time = time.time()
_list(10000)
end_time = time.time()
print("Spend:", end_time - start_time)
start_time = time.time()
_generator(10000)
end_time = time.time()
print("Spend:", end_time - start_time)
运行结果:
Spend: 3.6154024600982666
Spend: 0.0
参考链接:
https://blog.csdn.net/weixin_37720172/article/details/78482291