python高级编程和算法
import copy #a = ("a","b","c") #a = ["a","b","c"] a = {"a","b","c"} b =a c = copy.copy(a) d = copy.deepcopy(a) # print(b) # print(c) # print(d) # print(id(b)) # print(id(c)) # print(id(d)) """ 考点: 1.可变对象:列表和字典 2.不可变对象:一旦创建就不可修改的对象:元组,字符串,数字 """ class Person: #类对象 x = 5 y = 6 def __init__(self,x,y): self.x = x self.y = y print(self.x) print(self.y) def add(self): return self.x +self.y #实例对象 p = Person(1,2) p.z = 9 # 实例变量 # print(p.z) # #p1 = Person(3,4) # print(Person.add(Person(3,4))) # print(p.add()) # print("-------------------------") # print(p.z) # print(p.x) #实例变量 # print(p.y) #实例变量 # print("************************************") # print(Person.x) # 类变量 # print(p.add()) """ 考点: 1.类对象和实例对象 2.类变量和实例变量 """ # a *args **kwargs def function(a,*args,**kwargs): print(type(a)) print(type(args)) print(type(kwargs)) print(a) print(args) print(kwargs) # function(1,1,1) # function(6,7,8,9,b=2,c=3,d=4) """ 考点: 1.位置必须是一定的a,*args,**kwargs 2.类型分别是int,tuple,dict """ import time def runtime(function): def get_now_time(): print(time.time()) function() return get_now_time @runtime def run(): print("run") def runtime1(function): def get_now_time(*args): print(time.time()) function(*args) return get_now_time @runtime1 def run1(i): print(i) #run() #run1(1) # print("___________________________________") def runtime3(function): def get_now_time(**kwargs): print(time.time()) function(**kwargs) return get_now_time @runtime3 def run3(**kwargs): print(kwargs) #run3(a="aaa") def runtime4(function): def get_now_time(name,**kwargs): print(time.time()) function(name,**kwargs) return get_now_time @runtime4 def run4(name,**kwargs): print("名字==",name) print(kwargs) #run4("jiyanjiao",a="haha") """ 考点: 1.装饰器写法 2.不带参数装饰器 3.带参数的装饰器 """ def func1(): for i in range(1,5): return i def func2(): for i in range(1,5): yield i # print(func1()) # print(func2()) # yi = func2() # for i in yi: # print(i) # print("**************************") re =func2() # for i in re: # print(i) """ 考点: 1.yield 与 return区别 2.yield:生成器类型 """ # 题目:根据一个列表,创建一个新的列表,用一行代码 # lambada表达式 推导式 list1 = [1,2,3,4] result = map(lambda x: x*x,list1) # print(list(result)) # print("&&&&&&&&&&&&&&&&&&&&&&&&&&") # 用推导式来解决,列表推导式 list2 = [i*i for i in list1] #print(list2) list3 = [i*i for i in list1 if i>2] #print(list3) # 集合的推导式 打印结果无序,因为集合是无序的 list1 = {1,2,3,4} list4 = {i*i for i in list1} #print(list4) #字典的推导式 dict1 = { "key1":"value1", "key2":"value2" } keys = [key for key ,value in dict1.items()] #print(keys) key_value = {key:value for key,value in dict1.items()} #print(key_value) key_value_if = {key:value for key,value in dict1.items() if key=="key1"} #print(key_value_if) """ 考点: 1.推导式 [表达式,for循环,条件] """ ''' 常见排序方式: 插入排序,希尔排序,直接排序,堆排序 冒泡排序,快速排序,归并排序,基数排序 ''' # 给定一个列表,将这个列表进行排序,要求:时间复杂度要小于o(n^2) ''' 复杂度: 1.时间复杂度:指算法在计算的过程中所需要的计算工作量 2.空间复杂度:指算法在计算过程中所需要的内存空间 常见的时间复杂度: 常数阶O(1),对数阶O(log2n),线性阶O(n) 线性对数阶O(nlog2n),平方阶O(n^2),立方阶O(n^3) 随着问题的规模n,不断的增大,上述的时间复杂度就不断的增大,意味着算法的执行效率越低 ''' #冒泡排序的实现 # 相邻的两个数字进行比较,大的向下沉,最后一个元素时最大的 # 时间复杂度O(n^2) def bublle_sort(blist): count = len(blist) for i in range(0,count): for j in range(i+1,count): if blist[i] > blist[j]: blist[i],blist[j] = blist[j],blist[i] return blist blist = [8,5,90,31,1] s = bublle_sort(blist) #print(s) """ 快速排序: 思想:递归 列表中取出第一个元素作为标准,把比第一个元素小的都放在左侧,把比第一个元素大的都放在右侧 最小的在左边,最大的在右边,递归完成即排序结束 时间复杂度:O(nlog2n) """ # 使用递归+推导式写快速排序 def quick_sort(quick_list): if quick_list==[]: return [] else: first = quick_list[0] less = quick_sort([l for l in quick_list[1:] if l < first]) more = quick_sort([m for m in quick_list[1:] if m >=first]) return less + [first] + more quick_list = [8,5,90,31,1] print(quick_sort(quick_list))