标签:
1 class range(object) 2 | range(stop) -> range object 3 | range(start, stop[, step]) -> range object 4 | 5 | Return an object that produces a sequence of integers from start (inclusive) 6 | to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. 7 | start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. 8 | These are exactly the valid indices for a list of 4 elements. 9 | When step is given, it specifies the increment (or decrement).
大致意思就是前闭后开.产生一个有序序列 .
1 >>> for i in range(10,21): 2 print(i,"\n") 3 4 5 10 6 7 11 8 9 12 10 11 13 12 13 14 14 15 15 16 17 16 18 19 17 20 21 18 22 23 19 24 25 20
1 randint(a, b) method of random.Random instance 2 Return random integer in range [a, b], including both end points.// 包括两端的端点.
1 >>> import random 2 >>> for i in range(10): 3 print(random.randint(1,100)) 4 5 6 31 7 60 8 29 9 46 10 10 11 25 12 72 13 16 14 14 15 72
1 >>> g=lambda x : x*2+1 2 >>> g(5) 3 11
1 >>> g=lambda x : x*2+1 2 >>> g(5) 3 11 4 >>> def add(x,y): 5 return x+y 6 7 >>> add(3,4) 8 7 9 >>> f=lambda x,y:x+y 10 >>> f(3,4) 11 7
1 class filter(object) 2 | filter(function or None, iterable) --> filter object 3 | 4 | Return an iterator yielding those items of iterable for which function(item) 5 | is true. If function is None, return the items that are true.
1 >>> def odd(x): 2 return x%2 3 4 >>> # 声明了 Function . 5 >>> result=filter(odd(),range(10)) 6 Traceback (most recent call last): 7 File "<pyshell#4>", line 1, in <module> 8 result=filter(odd(),range(10)) 9 TypeError: odd() missing 1 required positional argument: ‘x‘ 10 >>> result=filter(odd,range(10)) 11 >>> result 12 <filter object at 0x02F809D0> 13 >>> list(result) 14 [1, 3, 5, 7, 9]
1 >>> list(filter(lambda x:x%2,range(10))) 2 [1, 3, 5, 7, 9]
1 >>> list(map(lambda x:x*2,range(10))) 2 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
标签:
原文地址:http://www.cnblogs.com/A-FM/p/5660679.html