python 基础知识学习
2014-12-28 10:59
309 查看
1、装饰器
2、传递参数
3、继承
4、列表排序
5、filter 找出索引为基数的数
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. 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.
6、快速排序
def warp(x): import time def f(func): def call_fun(*args,**kwargs): start_time = time.time() print 'func====',func,"args===",args,'kwargs===',kwargs func(*args,**kwargs) end_time = time.time() if end_time - start_time>x: print 'bad' else: print 'goods' return call_fun return f @warp(3) def ff(*args,**kwargs): import time print 'call func' time.sleep(4) 说明:装饰器的调用原理类似如下: warp(3)(ff)(*args,**kwargs) if __name__ == "__main__": ff("a","b")
2、传递参数
f(a,b,c="1",d="2",*args,**kwargs): pass f("a","b","c",c="dd") f("a","b",c="dd","9")
3、继承
class A(object): x= 1 class B(A): pass class C(A): pass print A.x,B.x,C.x B.x = 2 print A.x,B.x,C.x A.x = 3 print A.x,B.x,C.x
4、列表排序
l = [23,3,4,3,2,2,1,1,] sorted(list_a,lambda x1,x2:x1<x2 and 1 or -1) list_a.sort(lambda x,y:x<y and -1 or 1)
5、filter 找出索引为基数的数
Help on built-in function filter in module __builtin__:
filter(...)
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. 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.
<pre name="code" class="python">list_a = [12,213,22,2,2,2,22,2,2,32,] [elem[1] for elem in filter(lambda x:x[0]%2!=0, zip(range(1,len(list_a)),list_a))]
6、快速排序
def quick_sort(sort_list,result): if len(sort_list) ==1: return sort_list key_elem = sort_list.pop(0) left_list = [] right_list = [] for e in sort_list: if e>=key_elem: right_list.append(e) else: left_list.append(e) temp_result1 = [] temp_result2 = [] if left_list: quick_sort(left_list,temp_result1) if right_list: quick_sort(right_list,temp_result2) result.extend(temp_result1) result.append(key_elem) result.extend(temp_result2) return result if __name__ == "__main__": result = [] quick_sort([1,3,9,8,7,6,5,3,2,1,0,-9,123],result) print 'result============',result
相关文章推荐
- Python学习之一 Python基础知识
- Python2.7 学习笔记 (一)——基础知识
- Python学习笔记——基础知识
- Python学习笔记-第一章 基础知识
- Python学习笔记一 基础知识
- python 3.0学习笔记之二------python基础小知识
- Python学习笔记---基础知识
- [Python学习] 专题二.条件语句和循环语句的基础知识
- Python学习(三)——————基础知识汇总(二)
- [Python学习] 专题四.文件基础知识
- Linux Python基础知识学习,使用Eclipse与Pydev搭建python开发环境
- python学习笔记1:基础知识
- Python学习系列(二)(基础知识)
- 【转】python学习笔记-第1章节 基础知识
- python学习(1)--基础知识
- Python学习系列(二)(基础知识)
- python学习(基础知识)
- [Python学习] 专题三.字符串的基础知识
- <<Python基础教程>>学习笔记 | 第01章 | 基础知识