0x21 python basis, interest-based

01 Data Structure

1. The three basic types of variables:

  1. Integer (int)
  2. String (String)
  3. Float (float)
   we can use the type () to print out the type of a variable

2. The other four important types

  1. The set (set)

  2. List (list)

  3. tuple (tuple)

  4. dictionary (dict)

  dict set and built a hashing algorithm to find so fast

 

02 file read and write

. 1 GT100 = Open ( ' / Home / Liuyang / Desktop / 01- Light freedom /file.txt ' , ' W ' )
 2  for Line in Open ( ' / Home / Liuyang / Desktop / 01- Light Freedom / data. TXT ' ):
 . 3      # read line by line 
. 4      Line = line.strip ()
 . 5      # remove / n- 
. 6      ID_, name, Score = line.split ()
 . 7      # space-dividing line are assigned to the three variables 
. 8      Print ( ' ID: {name}: {} Score: {} ' .format (ID_, name, Score))
 . 9      # printing
10      gt100.write ( ' {}, {}, {} \ n- ' .format (ID_, name, Score))
 . 11      # row save

After this will be faster with a simple data using awk

 

03 Using the Module

1. third-party modules

  sys, re, etc.

  If you want to use for Web development django, flask, etc.

2. own modules

  import my_py_name  as mp

  Reference Methods mp.calc ()

 

04 Functional Programming

= the nums [1,2,5,8,10 ] 
GT5 = filter ( the lambda V: V>. 5 , the nums)
 # parameters of a filter function, a list 
# The parameters for the second order function each element is used If the condition is not satisfied continues True retention element iterative 
list (GT5)
 # list generator  
GT6 = [I for I in the nums IF I>. 6 ]
 Print (GT6)
 # doubling operation 
GT10 = [I for I in the nums IF I% == 2 0]
 Print (GT10)
java equivalent constructor __init__

05 a face questions + test ideas

  Two rows of through merging sequence list, a method: the sorted Method Method Two: Own a

#  A difficult interview questions 
A = [1,1,3,4,4 ] 
B = [2,3,4,6 ]
 DEF combin_sort_1 (A, B):
     # using current collection requirements and 
    return the sorted (SET ( a) | SET (B)) 

DEF combin_sort_2 (a, B): 
    RES = List (SET (a))
     for _B in B:
         # deduplication elements 
        IF _B in RES:
             Continue 
        for (IDX, _a) in the enumerate ( RES [:]):
             # this function will not work with a set of 
            # the enumerate function returns the position of the first element and the element value 
            IF _B>_a:
             # If the end of the list has reached the 
                IF IDX == len (RES) -1 : 
                    res.append (_B) 
                the else :
                     Continue 
            the else : 
                res.insert (IDX, _B) 
                BREAK 
    return RES
 Print (combin_sort_2 (A, B ))
 Print (combin_sort_1 (A, B))
# Thought to test the code 
Import Random
 DEF main ():
     # randomly generated two unequal length list, and sorts 
    # random.sample (range, number) 
    Frist the sorted = (random.sample (Range (100), Random .randint (3,6 ))) 
    SECOND = the sorted (random.sample (Range (100), the random.randint (3,6 ))) 
    
    comb_1 = combin_sort_1 (Frist, SECOND) 
    comb_2 = combin_sort_2 (Frist, SECOND) 
    
    # Comparative the results of the two methods 
    # Print (comb_1) 
    # Print (comb_2) 
    the Assert comb_1 == comb_2
     # If the results are not the same error will be 
    
IF  __name__ ==' __Main__ ' :
     for i in the Range (100 ): 
        main () 
    Print ( " successful test " )

 to sum up

1. The use of a set of thinking, and the direct current demand, to achieve the purpose of weight, sorted using a sorting function in

2. The list of simple insert, append

3.random.sample parameters, a list of the number of elements e

4. When traversing the list, the list will need to be changed if res [:] to copy data

5.assert test ideas

Use of 6 .__ main__

Guess you like

Origin www.cnblogs.com/liu247/p/10988805.html