2012年10月26日

python list排序

摘要: 很多时候,我们需要对List进行排序,Python提供了两个方法对给定的List L进行排序,方法1.用List的成员函数sort进行排序方法2.用built-in函数sorted进行排序(从2.4开始)这两种方法使用起来差不多,以第一种为例进行讲解:从Python2.4开始,sort方法有了三个可选的参数,Python Library Reference里是这样描述的cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative 阅读全文

posted @ 2012-10-26 21:18 百年孤寂dwn 阅读(328) 评论(0) 推荐(1) 编辑

python几个内置函数之-filter,map,reduce

摘要: ·filter()函数filter()函数包括两个参数,分别是function和list。该函数根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表,如下例所示:>>>a=[1,2,3,4,5,6,7]>>>b=filter(lambda x:x>5, a)>>>print b>>>[6,7]如果filter参数值为None,就使用identity()函数,list参数中所有为假的元素都将被删除。如下所示:>>>a=[0,1,2,3,4,5,6,7]b=f 阅读全文

posted @ 2012-10-26 14:31 百年孤寂dwn 阅读(2038) 评论(0) 推荐(0) 编辑

导航