码迷,mamicode.com
首页 > 编程语言 > 详细

python基础--零散知识点

时间:2018-02-26 23:11:15      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:bsp   ext   生成   运行   地址   越界   pos   lis   int   

假设一个列表的存有 数量很大的元素,但是在每一次运行时,通常只调用前面几个元素,这时为了优化,可以采用生成器.

isList = [x * x for x in range(10)]
print(isList)  # 结果[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

isGenerator = (x * x for x in range(10))
print(isGenerator)  # 这里显示的结果是生成器的地址<generator object <genexpr> at 0x0000028E39D79938>

print("result :  %d " % next(isGenerator))  # result :  0
print("result :  %d " % next(isGenerator))  # result :  1
print("result :  %d " % next(isGenerator))  # result :  4

for i in isGenerator:
    print(i)  # 依次是 9, 16, 25, 36, 49, 64, 81


for it in isGenerator:
    print("this time :  %d" % it)  # 结果为空,这时如果继续调用next(isGenerator),会报StopIteration(因为越界).



 

 

python基础--零散知识点

标签:bsp   ext   生成   运行   地址   越界   pos   lis   int   

原文地址:https://www.cnblogs.com/truthk/p/8476134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!