1.range()方法:
>>>range(5) [0, 1, 2, 3, 4] >>>a= ["heke","sdsdjs","sdksds"] >>>range(len(a)) [0, 1, 2]
迭代一个数字序列的话,可以使用range()函数,range()函数可以生成等差级数。
>>> xrange(5) xrange(5) >>> list(xrange(5)) [0, 1, 2, 3, 4] >>> xrange(1,5) xrange(1, 5) >>> list(xrange(1,5)) [1, 2, 3, 4] >>> xrange(0,6,2) xrange(0, 6, 2) >>> list(xrange(0,6,2)) [0, 2, 4]
xrange和range相比,不同点就在于xrange生成的不是一个数组,而是一个生成器
2.python enumerate函数
>>>import string s = string.ascii_lowercase e = enumerate(s) print s print list(e) abcdefghijklmnopqrstuvwxyz [(0, ‘a‘), (1, ‘b‘), (2, ‘c‘), (3, ‘d‘), (4, ‘e‘), (5, ‘f‘), (6, ‘g‘), (7, ‘h‘), (8, ‘i‘), (9, ‘j‘), (10, ‘k‘), (11, ‘l‘), (12, ‘m‘), (13, ‘n‘), (14, ‘o‘), (15, ‘p‘), (16, ‘q‘), (17, ‘r‘), (18, ‘s‘), (19, ‘t‘), (20, ‘u‘), (21, ‘v‘), (22, ‘w‘), (23, ‘x‘), (24, ‘y‘), (25, ‘z‘)]
同时需要index和value值的时候可以使用 enumerate
时间: 2024-10-09 22:25:42