标签:服务器 镜像 自动转换 cep 读写文件 有序 sha 元组 访问
set是一个无序且不重复的元素集合
访问速度快
天生解决重复问题
#!/usr/bin/env python3 # -*- coding:utf-8 -*- #深浅拷贝 import copy #浅拷贝 #copy.copy() #深拷贝 #copy.deepcopy() #赋值 #字符串,数字,无论深拷贝浅拷贝还是赋值,永远指向内存的一块地址 a1 = "fdkoslagj" a2 = a1 print(id(a1)) print(id(a2)) #其他,元组,列表,字典 n1 = {"k1":"wu","k2":123,"k3":["alex",456]} n2 = n1 print(id(n1)) print(id(n2)) #浅拷贝 n3 = copy.copy(n1) print(id(n1)) print(id(n3)) print(id(n1["k3"])) print(id(n3["k3"])) #深拷贝 n4 = copy.deepcopy(n1) print(id(n1)) print(id(n4)) dic = { "cpu":[80,], "mem":[80,], "dist":[80,] } print(‘before‘,dic) #深拷贝,迭代拷贝 new_dic1 = copy.deepcopy(dic) #改变内容,原变量也会变 #new_dic2 = copy.copy(dic) new_dic1[‘cpu‘][0]=50 print(dic) print(new_dic1)
#!/usr/bin/env python3 # -*- coding:utf-8 -*- s1 = set([11,22,33]) s2 = set([22,44]) #在s1中除去s1与s2的交集 ret1 = s1.difference(s2) #求s1和s2的差集 ret2 = s1.symmetric_difference(s2) ret3 = s2.symmetric_difference(s1) ret4 = s2.difference(s1) print(ret1) print(ret2) print(ret3) print(ret4) import collections #元素计数 对字典 obj = collections.Counter(‘kdfjsalgjkdlsajg:j‘) print(obj) #取前四个 ret = obj.most_common(4) print(ret) for item in obj.elements(): #输出所有元素 print(item) for k,v in obj.items(): #输出计数后的元素:个数 print(k,v) #有序字典 #将字典的key,保存在列表中 #有序字典 dic = collections.OrderedDict() dic[‘k1‘]=‘v1‘ dic[‘k2‘]=‘v2‘ print(dic) #将k1拿到最后 dic.move_to_end(‘k1‘) print(dic) ‘‘‘ #要加参数 dic.pop(‘k1‘) print(dic) #移除最后一个 dic.popitem() print(dic) ‘‘‘ dic.update({‘k1‘:‘v11‘,‘k10‘:‘v10‘}) print(dic) #默认字典,key值对应的是一个列表 dic = collections.defaultdict(list) dic[‘k1‘].append(‘alex‘) print(dic)
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import collections #可命名元组,自定义索引值 MytupleClass = collections.namedtuple(‘MytupleClass‘,[‘x‘,‘y‘,‘z‘]) obj = MytupleClass(11,22,33) print(obj.x,obj.y,obj.z) #双向队列 d = collections.deque() d.append(‘1‘) d.appendleft(‘10‘) d.appendleft(‘1‘) print(d) r=d.count(‘1‘) print(r) d.extend([‘xx‘,‘yy‘]) print(d) d.extendleft([‘zz‘]) print(d) #循环右移 d.rotate(1) print(d) d.rotate(2) print(d) #单项队列 import queue q = queue.Queue() q.put(‘123‘) print(q.qsize()) print(q.get())
#!/usr/bin/env python3 # -*- coding:utf-8 -*- ‘‘‘ 1.需要开启邮箱服务sendmail 2.邮箱服务器需要开启SMTP服务 ‘‘‘ def sendmail(): try: import smtplib from email.mime.text import MIMEText from email.utils import formataddr msg = MIMEText(‘邮件内容‘, ‘plain‘, ‘utf-8‘) msg[‘From‘] = formataddr(["发件人", ‘pythonxiao@126.com‘]) msg[‘To‘] = formataddr(["收件人", ‘329275108@qq.com‘]) msg[‘Subject‘] = "邮件主题" server = smtplib.SMTP("smtp.126.com", 25) server.login("pythonxiao@126.com", "xiaozhiqi2016") server.sendmail(‘pythonxiao@126.com‘, [329275108@qq.com, ], msg.as_string()) server.quit() except: return "失败" else: return ‘cc‘ ret = sendmail(msg) if ret == ‘cc‘: print("发送成功") else: print("发送失败")
#!/usr/bin/env python3 # -*- coding:utf-8 -*- #默认参数放后边 def show(a1,a2=999): print(a1,a2) show(111) #指定参数 def show1(a1,a2): print(a1,a2) show1(a2=123,a1=999) #参数为列表 def show2(arg): print(arg) n=[11,22,33,44] show2(n) # *arg 输出元组类型 def show3(*arg): print(arg,type(arg)) show3(1,33,44,55) # **arg 输出字典类型 def show4(**arg): print(arg,type(arg)) show4(n1=78) # *args **kwargs def show5(*args,**kwargs): print(args,type(args)) print(kwargs,type(kwargs)) show5(11,22,33,n1=88,alex=‘sb‘) #会将俩个都给*args l = [11,33,55] d = {‘n1‘:88,‘alex‘:‘sb‘} show5(l,d) #可以用一下方式实现*args -->l,**kwargs -->d show5(*l,**d)
#字符串格式化一 s1 = "{0} is {1}" result = s1.format(‘alex‘,‘2b‘) print(result) #字符串格式化二 s2 = "{name} is {acter}" result = s2.format(name=‘alex‘,acter=‘sb‘) print(result) #字符串格式化三 s3 ="{name} is {acter}" d = {‘name‘:‘alex‘,‘acter‘:‘shab‘} result = s3.format(**d) print(result)
#字符串格式化一 s1 = "{0} is {1}" result = s1.format(‘alex‘,‘2b‘) print(result) #字符串格式化二 s2 = "{name} is {acter}" result = s2.format(name=‘alex‘,acter=‘sb‘) print(result) #字符串格式化三 s3 ="{name} is {acter}" d = {‘name‘:‘alex‘,‘acter‘:‘shab‘} result = s3.format(**d) print(result)
r 只读模式(默认) w 只写模式。(不可读:不存在则创建,存在则删除内容) a 追加模式。(可读,不存在则创建,存在则则只追加内容) "+" 表示可同时读写某个文件 r+可读写文件,可读可写,可追加 w+写读 a+ 同a "U"表示在读取时,可以将\r \n \r\n 自动转换成\n(与r或r+模式同使用) rU r+U "b"表示处理二进制文件,(如FTP擦送上传ISO镜像文件,Linux可忽略,Windows处理二进制文件时需标注) rb wb ab
#写 f = open(‘test.log‘,‘w‘,encoding=‘utf-8‘) f.write(‘xuxiaopao‘) f.close() #读 read(2)是按照字符拿的 f = open(‘test.log‘,‘r‘,encoding=‘utf-8‘) ret = f.read(5) print(ret) f.close() #tell按照细节搞的,查看当前指针位置 f = open(‘test.log‘,‘r‘,encoding=‘utf-8‘) print(f.tell()) f.read(5) print(f.tell()) f.close() #seek 指定当前指针位置 f = open(‘test.log‘,‘r‘,encoding=‘utf-8‘) f.seek(3) #2会出错 ,3正常 ret = f.read(3) print(ret) f.close() #truncate 只保留前边的东西,后边的不要 f = open(‘test.log‘,‘r+‘,encoding=‘utf-8‘) f.seek(3) #print(f.read()) f.truncate() f.close()
标签:服务器 镜像 自动转换 cep 读写文件 有序 sha 元组 访问
原文地址:http://www.cnblogs.com/perl2py/p/6722299.html