1, delete the line end of the Alex,alex is not case-sensitive: Import res= "' Alex123ja654alexrain7alex890helloalex" #re. M makes ^ and $ by the beginning of the text, the end of the text into the line at the end of line, re. I do not distinguish the case of rule parameters; Re. s enables
.Can match \nm=re.sub (' alex$ ', ' ', S,count=0,flags=re. M+re. I) print (m)
********************** Split Line
**********************Add a prefix to each key of the JSON Ids_:import res= ' {"name": "Jerry", "age": + #同EditPlus的替换框, Group (0) for the entire lookup, group (1) for the 1th pair () Content in S=re.sub (' "(\w+?":) ', Lambda x: ' "Ids_ ' +x.group (1), s) print (s)
**************************************** Split Line ****************************************Bubble sort: t=[87, 10]for y in range (1,len (t)): #取出第y大; the last big one does not need to be sorted # The result of the loop within each round is to let the maximum go to the end, so the lower order does not need to add it for the X in range ( Len (t)-y): if t[x]>t[x+1]: tmp=t[x] t[x]=t[x+1] t[x+1]=tmp print (t)
**************************************** Split Line ****************************************Second order: Sort by score first, score the same as name: Class Student:def __init__ (self,name,score): Self.name=name self.score=s Core Def __str__ (self): Return ' (%s:%d] '% (self.name,self.score) l=[student (' Tim ', the total), Student (' Bob ', "," Student ( ' Alice ',]l=sorted (L,key=lambda x:[x.score,x.name]) print ([X.__str__ () for x in L])
**************************************** Split Line ****************************************Ordered Dictionary of Firstinfirstout: from collections Import Ordereddict class FIFO (ordereddict): Def __init__ (self, Capacity=3,*args, **kwargs): Self.capacity = Capacity #写到super () below, error no capacity attribute super (FIFO,
Self). __init__ (*args,**kwargs) #OrderedDict. __init__ (Self
, *args,**kwargs) def __setitem__ (self, Key, value): If key in Self:print (' Delete existed key: ', (Key,self[key])) Del Self[key] Elif len (self)
==Self.capacity: #表面是批量添加key, actually add each, with >= on the lily Front=self.popitem (
Last=falsePrint (' Delete front key: ', front) # like the constructor above, to use a function of the same name as the parent class, you must wait until the end to explicitly invoke it ordereddict.__setitem__ (Self , key, value) #super (FIFO, Self). __setitem__ (Key, Value) Fd=fifo (3,{' W ': 7, ' X ': 8, ' Y ': 9, ' Z ': 0}) print (FD) fd.update ([' A ', ' 1 '), (' B ', 2), (' C ', 3), (' d ', 4), (' E ', 5)]) print (FD) Fd.update ({' F ': 6, ' E ': 9}) print (FD)
**************************************** Split Line ****************************************Class inheritance in the Super:1, super Multiple inheritance of the construction and instance functions, with the common multiple inheritance: Breadth First, lazy mode, that is, after matching to the first class after the termination. 2, but if the first elder class also has super, is greedy mode: public ye class (such as Class E inherits B? C? D, and B? C? D andAllInherited the
A), executed only once; the super code of each parent class is the breadthShunSequence, after which the code is breadthInverseOrder. class a: def __init__ (self): print (" Enter a ") print (" Leave a ") class B (a): def __init__ (self): print ("Enter B") super (B, self). __init__ () print (" Leave B ") class C (A): def __init__ (self): print ("Enter C") super (c, self). __init__ () print ("Leave C") class D (A): def __init__ (self): print ("Enter D") super (D, self). __init__ () #此句取注或注释后, run once, observe the difference between the two results priNT ("Leave D") class E (B, C, D): def __init__ (self): print ("Enter e") super (E, self). __init__ () print ("Leave E") e ()
Regular, bubble sort, multilevel sort, FIFO