i foobar’d a fedora 4 machine a couple of days ago and when i went to mount the disk into another machine i get this beautiful error: mount: unknown filesystem type ̵……
[静下心来看python]-[16]-[partial]
[静下心来看python]-[15]-[filter]
1 2 3 4 5 |
from random import randint allNums = [] for eachNum in range(9): allNums.append(randint(1, 99)) print filter(lambda n: n%2, allNums) |
filter就是把前边的函数应用到没个元素上,然后true的留下,false的滚
1 2 3 4 5 6 7 8 9 10 |
>>> map((lambda x: x+2), [0, 1, 2, 3, 4, 5]) [2, 3, 4, 5, 6, 7] >>> >>> map(lambda x: x**2, range(6)) [0, 1, 4, 9, 16, 25] >>> [x+2 for x in range(6)] [2, 3, 4, 5, 6, 7] >>> >>>[x**2 for x in range(6)] [0, 1, 4, 9, 16, 25] |
map的作用是将函数应用到没个元素,得到一个由新元素组成的新的数组 [crayon-642258b……
[静下心来看python]-[14]-[lambda]
[静下心来看python]-[13]-[yield]
ansible 自定义filter_plugins
[静下心来看python]-[12]-[for… else…]
我们一般不会在其它语言中遇到for 循环和 while循环后跟 else 但是在Python中我们会 else的意义在于执行完所有循环之后,再执行else模块,注意,break会跳过else模块 看例子:
1 2 3 4 5 6 7 8 9 10 11 12 |
def showMaxFactior(num): count = num / 2 while count > 1: if num % count == 0 : print 'largest factor is :%s ' % count break count -= 1 else: print 'is Prive' for eachItem in range(20): showMaxFactior(eachItem) |
[静下心来看python]-[11]-[__seq__]
判断一个元素是否在seq中 obj [not] in sequence 两个类型相同的sequence推荐使用extend 来合并,效率要高于 + dir(list) 可以看到所有list的属性喝方法
1 2 |
>>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] |