Python range function

后端 未结 6 949
广开言路
广开言路 2021-02-07 05:53

Say I want to loop from 0 to 100 but with a step of 1/2. If you try

for i in range(0, 100, 0.5):
    whatever
         


        
6条回答
  •  迷失自我
    2021-02-07 06:34

    For large ranges it is better to use an generator expression than building a list explicitly:

     for k in ( i*0.5 for i in range(200) ):
         print k
    

    This consumes not much extra memory, is fast und easy to read. See http://docs.python.org/tutorial/classes.html#generator-expressions

提交回复
热议问题