八十、xrange类
1、定义:
class xrange(object) | xrange(stop) -> xrange object | xrange(start, stop[, step]) -> xrange object | | Like range(), but instead of returning a list, returns an object that | generates the numbers in the range on demand. For looping, this is | slightly faster than range() and more memory efficient.
2、解释:该方法与range(详见http://aoyanming.com/blog/display/31)类似, 不同的是xrange按要求返回的是可以生成一段变化的数字的对象。迭代速度以及内存效率要比range更快、更高效。
3、举例:
>>> xrange(1, 10, 3) xrange(1, 10, 3) >>> xrange(10) xrange(10) >>> range(1, 10) [1, 2, 3, 4, 5, 6, 7, 8, 9]
八十一、zip方法
1、定义:
zip(...) zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.
2、解释:此方法用处较大,建议掌握。此方法与map(详见:http://aoyanming.com/blog/display/28)的参数调用有点类似之处, zip返回一个元组的列表,列表的长度取决于参数中最短序列的长度。
3、举例:
>>> a = range(4) >>> b = range(7) >>> zip(a, b) [(0, 0), (1, 1), (2, 2), (3, 3)]
Copyright © 2021.aoyanming个人博客站
发表评论