Depth copy
Set is an unordered and non-repeating collection of elements
Fast access speed
Natural solutions to repetitive problems
#!/usr/bin/env python3#-*-coding:utf-8-*-#深浅拷贝import copy# Shallow copy #copy.copy () #深拷贝 #copy.deepcopy () #赋值 # string, number, Whether a deep copy is a shallow copy or an assignment, always point to a piece of memory address A1 = "FDKOSLAGJ" a2 = A1print (ID (A1)) Print (ID (A2)) #其他, tuple, list, dictionary n1 = {"K1": "WU", "K2": 123, "K3": [ "Alex", 456]}n2 = N1print (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": [+], "mem": [80,], "Dist": [80,]}print (' before ', dic) #深拷贝, iterative copy new_dic1 = Copy.deepcopy (DIC) #改变内容, the original variable will also change #new_dic2 = Copy.copy (DIC) new_dic1[' CPU '][0]=50print (DIC) print (NEW_DIC1)
Intersection difference set element count ordered dictionary default dictionary
#!/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# element counts to Dictionary obj = collections. Counter (' Kdfjsalgjkdlsajg:j ') print (obj) #取前四个ret = Obj.most_common (4) print (RET) for item in Obj.elements (): # Output all elements print (item) for K,V in Obj.items (): #输出计数后的元素: Number of print (K,V) #有序字典 # Key to the dictionary, save in List # ordered dictionary 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) #默认字典, The key value corresponds to a list of dic = Collections.defaultdict (list) dic[' K1 '].append (' Alex ') print (DIC)
Can name tuples, custom index values bidirectional queue loop Right Move single queue
#!/usr/bin/env python3#-*-coding:utf-8-*-import collections# can name tuples, custom index values 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 (' Ten ') 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 Queueq = queue. Queue () q.put (' 123 ') print (Q.qsize ()) print (Q.get ())
Mailbox Test
#!/usr/bin/env python3#-*-coding:utf-8-*-' ' 1. You need to turn on the mailbox service SENDMAIL2. Mailbox servers need to turn on the SMTP service ' Def sendmail (): try: Import Smtplib from email.mime.text import mimetext from email.utils import formataddr msg = mimetext (' message content ', ' plain ', ' utf-8 ') msg[' from ' = formataddr (["Sender", ' [email protected] ') ' msg[' to '] = FORMATADDR (["Recipient", ' [ Email protected] msg[' Subject '] = "Mail Subject" server = Smtplib. SMTP ("smtp.126.com") server.login ("[Email protected]", "xiaozhiqi2016") server.sendmail (' [Email Protected] ', [[email protected],], msg.as_string ()) server.quit () except: return "Failed" else: return ' cc ' RET = sendmail (msg) if ret = = ' CC ': print ("send Success") Else: print ("Send Failed")
function parameter Problem default parameter specify parameter parameter as list dictionary
#!/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 Output tuple type def show3 (*arg): print (Arg,type (ARG)) show3 (1,33,44,55) # **arg output dictionary type def show4 (**arg): print (Arg,type (ARG)) Show4 (n1=78) # *args **kwargsdef show5 (*args,**kwargs): print (Args,type (args)) print (Kwargs,type (Kwargs)) SHOW5 (11,22,33,n1=88,alex= ' SB ') #会将俩个都给 *ARGSL = [11,33,55]d = {' N1 ': ' Alex ': ' SB '}show5 (l,d) #可以用一下方式实现 *args-->l , **kwargs-->dshow5 (*l,**d)
String formatting
#字符串格式化一s1 = "{0} is {1}" result = S1.format (' Alex ', ' 2b ') print (result) #字符串格式化二s2 = "{name} is {acter}" result = S2.format (NA Me= ' Alex ', acter= ' SB ') print (result) #字符串格式化三s3 = "{name} is {acter}" D = {' name ': ' Alex ', ' acter ': ' Shab '}result = S3.format (**d) print (result)
Lambda,bool,all,any,excel table Handling eval, Filter,map,round,zip
#字符串格式化一s1 = "{0} is {1}" result = S1.format (' Alex ', ' 2b ') print (result) #字符串格式化二s2 = "{name} is {acter}" result = S2.format (NA Me= ' Alex ', acter= ' SB ') print (result) #字符串格式化三s3 = "{name} is {acter}" D = {' name ': ' Alex ', ' acter ': ' Shab '}result = S3.format (**d) print (result)
File processing
R Read-only mode (default) W write-only mode. (Unreadable: Does not exist then creates, there is delete content) a append mode. (readable, does not exist, then only append content)"+" means that a file can read and write at the same time R+ read-write file, readable writable, can be appended W+ write read a + a"U" means that when reading, \ r \ r \ n \ \ \ \ \ \ \ n (with R or r+ mode)is automatically converted to \ rUr +u "b" means processing binary files, (such as FTP wipe upload ISO image file, Linux can be ignored, Windows processing binary files need to be labeled) Rbwbab
View Code
#写f = open (' Test.log ', ' W ', encoding= ' Utf-8 ') f.write (' Xuxiaopao ') f.close () #读 read (2) is according to the word Funa f = open (' Test.log ', ' R ', encoding= ' utf-8 ') ret = F.read (5) print (ret) f.close () #tell按照细节搞的, view current pointer position f = open (' Test.log ', ' R ', encoding= ' utf-8 ') Print (F.tell ()) F.read (5) Print (F.tell ()) f.close () #seek Specify the current pointer position f = open (' Test.log ', ' R ', encoding= ' Utf-8 ') F.seek (3) # 2 error, 3 normal ret = F.read (3) print (ret) f.close () #truncate keep only the front, not the rear f = open (' Test.log ', ' r+ ', encoding= ' Utf-8 ') F.seek ( 3) #print (F.read ()) f.truncate () F.close ()
Python "Fourth" function basics