1. Bubble sort
Requirements: Please sort by small to large list [13, 22, 6, 99, 11]
Principle: The adjacent two values are compared, the larger values are placed on the right, and then the comparison!
LI=[39,11,43,88,765,9]
For I in range (Len (LI)):
For n in range (I,len (LI)):
If LI[N]>LI[N+1]:
Li[n],li[n+1]=li[n+1],li[n]
Print (LI)
2. Select sort
3. Recursion
Recursive: Loop calls the function itself, exits according to the condition
Def AB (a):
If a==1:
Return 1
Return A*ab (A-1)
Print (AB (7))
4. Reflection
The reflection functionality in Python is provided by the following four built-in functions: Hasattr, GetAttr, SetAttr, delattr, and four functions for internal execution of objects: Checking for the inclusion of a member, getting members, setting members, deleting members
classFoo(object):
def__init__(self):
self.name =‘wupeiqi‘
deffunc(self):
return‘func‘
obj =Foo()
# #### 检查是否含有成员 ####
hasattr(obj, ‘name‘)
hasattr(obj, ‘func‘)
# #### 获取成员 ####
getattr(obj, ‘name‘)
getattr(obj, ‘func‘)
# #### 设置成员 ####
setattr(obj, ‘age‘, 18)
setattr(obj, ‘show‘, lambdanum: num +1)
# #### 删除成员 ####
delattr(obj, ‘name‘)
delattr(obj, ‘func‘)
How to import the M module inside the Lib directory
def run ():
Inp=input (' Please enter URL: ')
M,f=inp.split ('/')
Obj=__import__ (M)
If Hasattr (obj,f):
Func=getattr (OBJ,F)
Func ()
Else
Print (' 404 ')
Run ()
5. Module
Hashlib Module
For cryptographic related operations, instead of the MD5 module and the SHA module, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm
Import Hashlib
Obj=hashlib.md5 (bytes (' adfdf ', encoding= ' utf-8 '))
Obj.update (bytes (' 123 ', encoding= ' utf-8 '))
Result=obj.hexdigest ()
Print (Result)
6. Regular expressions
The RE module is used for the operation of a Python regular expression.
Character:
. Match any character other than line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
Number:
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
ip:^ (25[0-5]|2[0-4]\d|[ 0-1]?\d?\d) (\. ( 25[0-5]|2[0-4]\d| [0-1]?\d?\d]) {3}$ phone number: ^1[3|4|5|8][0-9]\d{8}$
1. Match (pattern, string, flags=0)
Matches the specified content from the starting position based on the model to the string, matching a single
- Regular expressions
- The string to match
- Flag bit, used to control how regular expressions are matched
Import reobj = Re.match (' \d+ ', ' 123uuasf ') if obj: print Obj.group ()
2. Search (Pattern, string, flags=0)
Matches the specified content in a string according to the model, matching a single
Import reobj = Re.search (' \d+ ', ' u123uu888asf ') if obj: print Obj.group ()
3. Group and Groups
A = "123abc456"
Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group ()
Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (0)
Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (1)
Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Group (2)
Print Re.search ("([0-9]*) ([a-z]*) ([0-9]*)", a). Groups ()
4, FindAll (pattern, string, flags=0)
Both of these methods are used to match single values, that is, only one of the strings can be matched, and if you want to match all eligible elements in a string, you need to use FindAll.
Import reobj = Re.findall (' \d+ ', ' fa123uu888asf ') print obj
5, sub (pattern, REPL, String, count=0, flags=0)
Used to replace a matching string
Content = "123abc456" new_content = re.sub (' \d+ ', ' SB ', content) # new_content = re.sub (' \d+ ', ' SB ', content, 1) print New_co Ntent
6, Split (pattern, String, maxsplit=0, flags=0)
Grouping according to a specified match
Content = "' 1-2 * ((60-30+1* (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2)) '" New_content = Re.split (' \* ', content ) # new_content = Re.split (' \* ', content, 1) print new_content
Python-day6 Common algorithm python built-in module