''' Exercising questions '''
# 1 traversal generation array
i = []
a = range ( 10 )
i. extend (a)
print (i[ :- 2 ])
#2 bubble sorting
lists = [ 1 , 34 , 34 , 56 , 3 , 55 , 766 ]
Count = len (lists)
for i in range ( 0 , count) :
for j in range (i + 1 , count) :
if lists[i] > lists[j] :
lists[i], lists[j] = lists [j], lists[i]
print (lists)
#3 , traversal array, bubble sort
lists = [ 1 , 3 , 65 , 678 , 8 , 87 , 6 , 5 , 4 , 4 , 43 , 34 , 342 , 53 , 5 , 34 , 634 , 6 , 345 ]
a = []
a. extend (lists)
print (a)
Num = len (lists)
for i in range ( 0 , num) :
for j in range (i + 1 , num) :
if lists[i] > lists[j] :
lists[i], lists[j] = lists [j], lists[i]
print (lists)
# 4 , the list goes to the collection and performs operations on it
list1 = [ 1 , 2 , 5 , 1 , 3 , 55 , 76 ]
a = set (list1)
print (a)
List2 = [ 100 , 2 , 3 ]
b = set (list2)
print (b)
print (a & b)
print (a | b)
print (a - b)
#5 , dictionary
c = { 'x' : 1 , 'y' : 2 }
d = dict ( x = 1 , y = 2 )
print (c[ 'x' ])
print (d[ 'x' ])
print (c. keys ()) # output key
print (c. values ()) # Output value
# 6 , list pushed to type
a = [x for x in range ( 10 ) if x < 6 ]
print (a)
Print ( ' Square is: ' , [x * x for x in range ( 10 ]])
print ( ' Sum of squares :' , sum (x * x for x in range ( 10 ))) # list pushed to style
# 7. Generator
def a ( N ) :
for i in range ( 1 , N ) :
yield i
For i in range ( N , 0 , - 1 ) :
yield i
For i in a ( 10 ) :
print (i)
# 8 double queue (stack)
import collections
Lists = [ 1 , 2 , 3 , 4 , 5 ]
d = collections. deque (lists) # Containers that can be edited at both ends
d. append ( 6 ) # Add value at the end of
d. appendleft ( 0 ) # Add value at the end and end
print (d)
D2 = collections. deque ( '@' )
List1 = [ 9 , 8 , 7 , 6 ]
D2. extend (list1) # Add this list at the end of
d2. extendleft (list1)
print (d2)
D2. pop ()
print ( ' Deletes the first number on the right: ' , d2. pop ())
D2. popleft ()
print (d2)
Import collections
List1 = [ 1 , 2 , 3 , 4 , 5 ]
D1 = collections. deque (list1)
for i in range ( 4 , 0 , - 1 ) :
d1. append (i)
print ( ' Double queue: ' , d1)
I = 0
the while True :
I + = . 1
the try :
Print ( ' of % d with value: % d penultimate % d with value: % d' % . (I, D1 popleft . (), I, D1 POP ()))
except IndexError :
break
# 9 , lowercase uppercase
item = 'my name is Luzaofa'
print (item. upper ()) # lowercase uppercase
# 10 , regular expression
s = 'luzao fa do homework'
a = s. capitalize ()
b = s. count ( 'q' )
c = s. expandtabs ( 4 )
D1 = s. index ( 'f' )
d = s. find ( 'f' )
e = s. lstrip ()
E1 = s. rstrip ( '1' )
h = s. replace ( ' ' , '' )
H1 = s. strip ( '' )
g = s. title ()
print (g)
Import re
a = 'Luzaofa123luwdyuta66878zlyguygao889fa'
pattern = re. compile ( r'(\d+)' , re.S) # re.I ignore case
= item ' . join . (pattern. split (a, 2 )) # slice limited position
Item1 = pattern. sub ( '' , a) # Delete all the numbers where you can write the specific position
pattern1 = re. compile ( r'^l(\w+)a$' , re.I) # Match all to l Start with aThe end of the string
item2 = pattern. sub ( '' , a). upper () # uppercase conversion
print (item2)
#11 . Remove all HTML tags
from lxml import html
Content = open ( 'C:/Users/Administrator/python/work/HTML1.txt' , 'r' , encoding = 'utf-8' ). read ()
print (content)
Page = html. document_fromstring (content) # Parse file
text = page. text_content () # Remove all labels
print (text)
#12 Exception Handling, Explicitly Throws an Exception (sequential execution in three sections)
a = 'zao'
b = 'p'
c = 3
try :
try :
a = int (a) / 1
print (a)
except :
print ( "Error a" )
raise TypeError
except :
try :
a = int (b) / 1
print (a)
except :
print ( "Error b" )
# raise TypeError
finally :
print (c)
#13 , json serialization and deserialization
import json
Name = 'luzaofa'
age = 20
university = 'SBS'
dic = {}
Content = {}
Content[ 'Name' ] = name
Content[ 'Age' ] = age
print (content)
Dic[ 'University' ] = university
Content[ 'School' ] = json. dumps (dic)
print (content)
a = json. loads (content[ 'School' ])
print (a[ 'University' ])
# 14 、 system time function
import time
a = time. clock ()
print (time. asctime ()) # Returns the current time of the system
b = time. clock ()
print (b - a)
#15 . The random function
import os
a = os. urandom ( 1 ) # physical random number, return N random bytes, read print (a[ 0 ]) from source of password strength
#16 exception handling and judgment function
name1 = { 'Name' : 'luzaofa1' }
Name2 = { 'Name' : 'luzaofa2' }
Name3 = { 'Name' : 'luzaofa3' }
Item = '1'
try :
if item in name1[ 'Name' ] :
print ( '%s in %s' % (item, name1[ 'Name' ]))
Elif item in name2[ 'Name' ] :
print ( '%s in %s' % (item, name2[ 'Name' ]))
Else :
print ( '%s in %s' % (item, name3[ 'Name' ]))
Except :
print ( 'None' )