在
Python 中,range 和 xrange 均可以用来做迭代的范围,不过 range 返回的是 list,而 xrange 则返回一个 xrange object。
for x in range(1000):
will
generate a list of one thousand elements, and will then loop through each of them in turn
for x in xrange(1000):
will genarate one thousand integers one by one, passing each to the variable x in turn.
for x in xrange(1000):
will genarate one thousand integers one by one, passing each to the variable x in turn.
xrange
的效率比 range 高。