python-->>函数
#对于python,一切事物都是对象,对象基于类创建
#help(list) #将列表中的,功能详细的列出来
#set是一个无序且不重复的元素集合
#集合可以去除重复
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import collections c1 = collections.Counter('sassaassadff')#counter 相当于类,和创建一个字典相同,具备字典的功能+自己的功能 c1.most_common(3)#至少出现过三次OR三次以上的,符合的print出来,不符合不会打print print(c1) obj = collections.Counter('sjdassasfasfsdgdsg') print(obj) ret = obj.most_common(4) #前4个打印出来 print(ret) for k in obj.elements(): #原生的值。 print(k) for k,v in obj.items(): # print(k,v)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
old_dict = { "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }, "#2":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }, "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 } } new_dict = { "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 800 }, "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }, "#4":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80} } old = set(old_dict.key()) new = set(new_dict.key()) update_set = old.intersection(new) #更新的 dele_set = old.difference(update_set) #删除的 add_set = new.difference(update_set) #增加的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
MytupleClass = collections.namedtuple('MytupleClass',['x', 'y', 'z']) print(help(MytupleClass)) obj = MytupleClass(11,22,33) print(obj.x) #讲obj里面的11,22,33,赋值给x,y,z print(obj.y) print(obj.z)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
####拷贝 import copy #copy.copy() #浅拷贝 #copy.deepcopy()#深拷贝
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import copy n1 = 123 #n2 = 123 n3 =copy.copy(n1) ####只拷贝第一层列表,后面的都不拷贝。 print(id(n1)) #493868944 #打印的ID相同,浅拷贝第一层列表,后面的都不拷贝。 print(id(n3)) #493868944
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import copy n1 ={'k1':123,'k2':'haha','k3':['xixi',123]} #n2 = n1 n3 = copy.deepcopy(n1) #深拷贝,全部拷贝,除了对象。 print(id(n1)) #35379528 print(id(n3)) #35380040 print(id(n1['k3'])) #36139592 print(id(n3['k3'])) #36107528
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import copy dic = { 'cpu':[80], 'disk':[80], 'men':[80] } print('brefore',dic) #brefore {'disk': [80], 'men': [80], 'cpu': [80]} 原来的值 #print(dic) #new_dic = copy.copy(dic) #浅拷贝,修改完cpu的元素后,dic会变,new_dic会变。 new_dic = copy.deepcopy(dic) #深拷贝,修改完cpu的元素后,会重新开辟一块空间,dic 不变,new_dic会变。 new_dic = ['cpu'][0] = 50 print(dic) #{'disk': [80], 'men': [80], 'cpu': [80]}## 原来的值 print(new_dic) #{'disk': [80], 'men': [80], 'cpu': [50]} ##修改过后的值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def mail(): #创建函数:mail() n = 1 n +=1 print(n) mail() #使用函数。 mail #这个是函数名 f = mail f() #和mail()相同,都是同一个作用。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import smtplib from email.mime.text import MIMEText from email.utils import formataddr def mail(): ret = True #如果发送成功则出现Ture try: #如果下面执行不成功,则执行下面的except Exception: msg = MIMEText('你','plain','utf-8') msg['From'] = formataddr(["武沛齐",'wptawy@126.com']) msg['To'] = formataddr(["走人",'424662508@qq.com']) msg['Subject'] = "主题" server = smtplib.SMTP("smtp.126.com", 25) server.login("wptawy@126.com", "WW.3945.59") server.sendmail('wptawy@126.com', ['947792121@qq.com',], msg.as_string()) server.quit() except Exception: #执行他, ret = False return ret #定义返回的值为ret ret如果邮件执行成功则被变量ret = Ture ,如果失败,则ret = False。 ret = mail() if ret: #如果等Ture ,则打印‘发送成功’ print('发送成功') else: #否则‘发送失败’ print('发送失败')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import smtplib from email.mime.text import MIMEText from email.utils import formataddr def mail(user): #user:等于形式参数 ret = True #如果发送成功则出现Ture try: #如果下面执行不成功,则执行下面的except Exception: msg = MIMEText('你你你','plain','utf-8') msg['From'] = formataddr(["武沛齐",'wptawy@126.com']) msg['To'] = formataddr(["走人",'424662508@qq.com']) msg['Subject'] = "主题" server = smtplib.SMTP("smtp.126.com", 25) server.login("wptawy@126.com", "WW.3945.59") server.sendmail('wptawy@126.com', [user,], msg.as_string()) server.quit() except Exception: #执行他, ret = False return ret #定义返回的值为ret ret如果邮件执行成功则被变量ret = Ture ,如果失败,则ret = False。 ret = mail('424662508@qq.com') #实际参数()括号里面的是这个实际参数, ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') ret = mail('424662508@qq.com') if ret: #如果等Ture ,则打印‘发送成功’ print('发送成功') else: #否则‘发送失败’ print('发送失败')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(a1,a2=123,a3=999,a4=222): #默认参数 print(a1,a2) #111 123 #打印结果为这个,注意写的格式 show(111)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(a1,a2): print(a1,a2) show(a2=123,a1=2222) #指定参数可以顺序不一。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(*arg): #动态参数, 将所有的单个元素自动生成tuple元祖 print(arg,type(arg)) show(1,2,23,4,5,6,77) #(1, 2, 23, 4, 5, 6, 77) <class 'tuple'>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(**arg): #两个动态参数,将组合自动生成一个dict字典。 print(arg,type(arg)) show(n1=1,n2=2,n3=3) #{'n2': 2, 'n3': 3, 'n1': 1} <class 'dict'>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(*args,**kwargs): #一个动态参数和两个动态参数的组合,注意:格式为一个*号的在前面,两个**号的在后面,规定 print(args,type(args)) #(1, 2, 3, 4, 5, 6) <class 'tuple'> print(kwargs,type(kwargs)) #{'n2': 3, 'n3': 4, 'n1': 1} <class 'dict'> show(1,2,3,4,5,6,n1=1,n2=3,n3=4) #单个元素在前门,组合元素在后面,规定。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def show(*args,**kwargs): print(args,type(args)) #(11, 22, 33, 44, 55) <class 'tuple'> print(kwargs,type(kwargs)) #{'n2': 33, 'n1': 12} <class 'dict'> l = [11,22,33,44,55,] d = {'n1':12,'n2':33} show(*l,**d) #这形式将L赋值给*args,将**d赋值给**kwargs.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s1 = '{0} is {1}' l = ['alex','sb'] #result = s1.format('alex','sb') result = s1.format(*l) # 通过这种方式也可以将内容输入一个*的动态参数中。 print(result) #alex is sb
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s1 = '{name} is {act}' l = {'name':'alex','act':'sb'} #result = s1.format(name='alex',act='sb') result = s1.format(**l) # 通过这种方式也可以将内容输入两个**的动态参数中。 print(result)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
###lambda表达式,简单函数的表达方式。 func = lambda a: a+1 #创建形式参数A ,函数内容 a+1 并把结果给return ret = func(99) print(ret) #100
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import collections 2 newqueue = collections.deque(['a','b','c']) 3 4 print(newqueue) 5 newqueue.append(['d1','d2']) #追加一个元素到队列 6 print(newqueue) 7 8 newqueue.appendleft('a1') #追加一个元素到队列左侧 9 newqueue.appendleft('a2') #追加一个元素到队列左侧 10 print(newqueue) 11 12 newc = newqueue.count('a') #对队列某个元素进行计数 13 print(newc) 14 15 newqueue.extend(['e','f','g']) #扩展队列元素 16 print(newqueue) 17 18 newqueue.extendleft(['a11','a22','a33']) #从左侧开始扩展队列 19 print(newqueue) 20 21 newqueue.insert(2,'aa22') #插入到下标2的位置 22 print(newqueue) 23 24 newqueue.reverse() #顺序反转 25 print(newqueue) 26 27 newqueue.rotate(4) #将队列末尾4个元素反转到队列左侧 28 print(newqueue)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import queue newqueue = queue.Queue(2) #设置队列长度为2,也就是队列里只有两个任务 newqueue.put(['1','2']) # 放入一个任务 newqueue.put(2) # 放入第二个任务 isempty = newqueue.empty() #判断队列是否空 isfull = newqueue.full() # 判断队列是否满了 get1 = newqueue.get() #获取第一个任务 get2 = newqueue.get() #获取第二个任务 print(get1) print(get2) isfull2 = newqueue.full() #判断队列是否满了,因为已经把任务取出来了,所以这时队列应该是没有满 isempty2 = newqueue.empty() #判断队列是否为空 print(isfull) print(isfull2) print(isempty) print(isempty2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 dic1 ={'k1':'v1','k2':'v2'} 2 dic2 ={'k1':'vv','k2':'v2','k3':'v3'} 3 s1 = set(dic1) 4 print(s1) 5 s2 = s1.update(dic2) 6 print(s1) #将会对集合本身进行扩展,如果对象本身不存在这个传入的元素,则添加到对象本身的元素集合
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = {'a','s'} name.add('s') print(name) #返回结果为{'s', 'a'} name.add('xixi') print(name) #返回结果为{'xixi', 'a', 's'}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = {'a','s'} name.clear() print(name) #返回结果为set(),为空,对象都被干掉了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
yu = ('a','b','exls') name = {'exls','a','b'} name.difference_update(yu) print(name) #返回结果为set(),将yu和name里面的相同的元素删除,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
yu = {'a','b','exls'} yu.discard('exls') print(yu) #{'a', 'b'} 将我指定的exls 移除。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
yu = ('a','b','exls') name= {'a','b','ssd'} li = name.intersection(yu) print(li) #返回结果为{'a', 'b'},取得是交集,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
yu = ('a','b','exls') name= {'a','b','ssd'} li = name.isdisjoint(yu) print(li) #False 没有交集返回Ture,有交集返回False
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
old_dict = { "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#2":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, } new_dict = { "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 800 }, "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#4":{ 'hostname':'c2', 'cpu_count': 2, 'mem_capicity': 80 }, } 获取三个列表: 1. 需要更新的列表 update_list 2. 需要删除的列表 del_list 3. 需要增加的列表 add_list 代码实现: #!/usr/local/env python3 old_dict = { "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#2":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, } new_dict = { "#1":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 800 }, "#3":{ 'hostname':'c1', 'cpu_count': 2, 'mem_capicity': 80 }, "#4":{ 'hostname':'c2', 'cpu_count': 2, 'mem_capicity': 80 }, } #设置set old_set = set(old_dict.keys()) new_set = set(new_dict.keys()) #更新的set update_set = new_set.intersection(old_dict) delate_set = old_set.difference(update_set) add_set = new_set.difference(update_set) update_list = [] del_list = [] add_list = [] # print(update_set) # print(delate_set) # print(add_set) for i in update_set: update_list.append({i:new_dict[i]}) print('需要更新的列表:%s' % update_list) for i in delate_set: del_list.append({i:old_dict[i]}) print("需要删除的列表:%s" % del_list) for i in add_set: add_list.append({i:new_dict[i]}) print("需要增加的列表:%s" % add_list)
###collections counter计数器,同时也能做有序字典。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import collections dic = collections.OrderedDict() ###有序字典是通过OrderedDict函数来做的 dic['k1'] = 'v1' dic['k2'] = 'v2' dic['k3'] = 'v3' print(dic)