xrange与range的区别
在for循环中使用xrange与range函数,利用help函数,查的他们的用法如下:
range:
<span style="font-family:Comic Sans MS;">range(...)1、range()有三个参数,start, stop, step, 其中stop必须给定,start和step是可选的,但是若给出step,必须要给定start。
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a <span style="color:#ff0000;">list </span>containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.</span><span style="font-family:KaiTi_GB2312;">
</span>
2、range()返回的是一个列表,列表中的数是一个等差数列(递增或者递减)
xrange:
<span style="font-family:Comic Sans MS;font-size:18px;">class xrange(object)
| xrange(stop) -> xrange object
| xrange(start, stop[, step]) -> xrange object
|
| Like range(), but instead of returning a list, returns an <span style="color:#ff0000;">object</span> that
| generates the numbers in the range on demand. <span style="color:#ff0000;">For looping, this is
| slightly faster than range() and more memory efficient.</span>
|
| Methods defined here:
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __len__(...)
| x.__len__() <==> len(x)
|
-- More --</span>
从range和xrange的用法中,可以看出,他们两个的用法是一样的,但是:
1、range返回的是一个列表,而xrange 返回的是一个对象
2、xrange只是在需要的时候,在指定范围内产生一个值,而range是一次性产生所有的数,存放在列表中
3、xrange占有的内存空间比range低,其效率也更高。
lambda表达式:
lambda表达式实际上是一个函数对象,它可以出现在def不能出现的地方。lambda表达式是:关键字lambda,后面是一个或者多个参数(用逗号隔开),然后是紧跟一个冒号,冒号后面是一个表达式。例如:
g = lambda x, y : x * y输出为:6
print g(2, 3)
map()、reduce()、filter()
1、Map()函数---function函数作用于序列的每个元素然后返回一个集合
<span style="font-family:Comic Sans MS;font-size:18px;">>>>
>>> help(map)
Help on built-in function map in module __builtin__:
map(...)
map(function, sequence[, sequence, ...]) -> list
<strong><span style="color:#ff0000;">Return a list of the results of applying the function to the items of
the argument sequence(s)</span></strong>. If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).</span>
map()函数格式为:map(function, sequence[, sequence, ...]) -> list。其中function会对参数序列sequence中的每个元素都作用一遍,然后返回一个集合,例如:
print map(lambda x : x**2, [y for y in range(10)])
输出:[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
要注意map中function的参数数量要和序列的数量保持一样,如:
print map(lambda x, y: x * y, [n for n in range(10)], [k for k in range(10, 20)])
从两个序列对应地取值作为function的参数
输出:[0, 11, 24, 39, 56, 75, 96, 119, 144, 171]当function为None时,map的作用跟zip函数一样,即:把任意多的序列作为参数,然后返回一个元组列表。如:
print map(None, [n for n in range(10)], [k for k in range(10, 20)])
输出:[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]
若function为空,并且给出任意多的序列,其中序列的长度不一定,那么,zip会以最短长度的序列为准,组成元组列表,而map不会,短的序列在生成元组时,会自动填成None。如:
>>> name = ["zhang", "wang", "li", "zhao"]
>>> age = [20, 30, 40, 50]
>>> sex = ["male", "famale", "male", "famale"]
>>> sal = [5000, 7000, 6000]
>>> zip(name, age, sex, sal)
[('zhang', 20, 'male', 5000), ('wang', 30, 'famale', 7000), ('li', 40, 'male', 6000)]
>>> map(None, name, age, sex, sal)
[('zhang', 20, 'male', 5000), ('wang', 30, 'famale', 7000), ('li', 40, 'male', 6000), ('zhao', 50, 'famale', None)]
2、reduce()函数---累积运算
help(reduce)给出的定义为:
<span style="font-family:Comic Sans MS;font-size:18px;">reduce(...)
reduce(function, sequence[, initial]) -> value
<span style="color:#ff0000;">Apply a function of two arguments cumulatively to the items of a sequence,
from left to right,</span> so as to reduce the sequence to a <span style="color:#ff0000;">single value</span>.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.</span>
reduce函数的定义格式为:reduce(function, sequence[, initial]) -> value。其作用是,function对序列作累积运算。1、如果当initial给出,首先是函数作用于序列的第一个元素和initial,其结果再与序列的第二个元素作function,一次类推。如:
>>> reduce(lambda x, y: x*y, [1, 2, 3, 4], 2)等价于((((2*1)*2)*3)*4) = 48
48
2、如果initial没有给出那么,function直接作用于序列的第一个和第二个元素,其结果再与第三个元素作function,以此类推。
>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
>>>
等价于:((((1+2)+3)+4)+5)3、有时候可以替代递归函数,如求解n!问题。
3、filter()---过滤或者筛选
<span style="font-family:Comic Sans MS;">>>> help(filter)
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
<span style="color:#ff0000;">Return those items of sequence for which function(item) is true.</span> If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.</span>
filter函数具有类似一个过滤或者筛选的作用,它跟map,reduce一样,第一个function函数(如果存在)会作用于,第二个参数序列的每一个元素。不同的是,filter会根据function(item)的结果是true还是false来决定当前item元素是不是留下来。如下,让元素为奇数的留下来:
>>> filter(lambda x: x&1 != 0, [1, 2, 3, 4, 5, 6])
[1, 3, 5]
如果function函数不存在那么,参数序列的每个非零的元素都将作为true,返回。如下:
>>> filter(None, [1, 2, 3, 4, 5, 6])
[1, 2, 3, 4, 5, 6]
>>> filter(None, [0, -2, 3, 4, 5, 6])[-2, 3, 4, 5, 6]如果sequence 是一个元组(tuple)或者是一个(string),那么返回相应的类型 ,否则返回一个列表。