After the written test may have to face questions remember --Python Summary

1. String Processing

  The number string replaced by twice its value, for example:

Before modification: "AS7G123m (d) F77k"

Modified: "AS14G246m (d) F154k"

   Personal thoughts: first expression which will match with positive numbers out for operation by 2, and then the string is cut according to figures of them, get a list of characters, numbers, and eventually will be multiplied by the original character after 2 splicing get the final result. (My head is stupid, I can not think of anything else, if you have a better and more convenient way, hoping to share with you!)

. 1  Import Re
 2  
. 3 text = " AS7G123m (D) F77k " 
. 4 the nums = the re.findall (R & lt ' (\ + D) ' , text)   # remove the numeric string 
. 5 double_nums = [2 * int (I) for I in the nums]    # multiplied by 2 
. 6 the_str = []   # character list 
. 7  for I in the nums:
 . 8      the_str.append (text.split (I,. 1 ) [0])
 . 9      text = text.split (I,. 1) [. 1 ]
 10 the Result = ""   # 结果
11 for i in range(len(double_nums)):
12     result += the_str[i] + str(double_nums[i])
13 result += text
14 print(result)

 

2.Python pass parameter values ​​are passed by reference or pass?

   The answer is that Python pass arguments are passed by reference, it should prove to be passed by reference it? Can refer to the following example:

1 def f(x):
2     print(id(x))
3     
4 
5 a = 1
6 print(id(a))
7 f(a)
8 # 140716760159264
9 # 140716760159264

  Here were printed two addresses, one is a target address is the address passed in a parameter x, you can see two addresses are the same, which also shows pass parameters passed by reference in Python used! Note that: for immutable types, will not affect its operation in function of the original object, but the variable type, its operation in a function might change its value , as follows:

1) the parameters passed are immutable type:

. 1  DEF F (X):
 2      X + = " 666 "   # here creates a new object 
. 3      Print (X)
 . 4  
. 5  
. 6 S = " 777 "   # string immutable 
. 7  Print (S)
 . 8  F (S)
 . 9  Print (S)
 10  
. 11  # 777 
12 is  # 777 666 
13 is  # 777

2) incoming parameter is the variable type:

. 1  DEF F (X):
 2      x.append (. 4)   # modify the value of the original object 
. 3      Print (X)
 . 4  
. 5  
. 6 S = [. 1, 2,. 3]   # listing variable 
. 7  Print (S)
 . 8  F (S )
 . 9  Print (S)
 10  
. 11  # [. 1, 2,. 3] 
12 is  # [. 1, 2,. 3,. 4] 
13 is  # [. 1, 2,. 3,. 4]

 

3. those things deep and shallow copy copy

  In Python, a shallow copy and a deep copy is sure to make clear in! For shallow vs. deep copy, it can be understood:

  1) a shallow copy: create an object, but is included in the items comprising references to the original object, if the object reference which is modified, will change the original object.

  2) deep copy: create an object, and recursive copy objects contained in the original object, modify the data at this time does not affect the original object.

  Contained in the following code of the assignment, shallow vs. deep copy, the assignment that is referenced object in Python, so the c-operation is the original object a is operated for a shallow copy of the object b and d, a reference to which the operation a change of the object, e deep copy target is unrelated to the operation of the original object a.

. 1  Import Copy
 2  
. 3 A = [. 1, [2],. 3 ]
 . 4 B = A [:]   # slice operation, shallow copy 
. 5 C = A   # assignment, i.e. a reference 
. 6 D = a.copy ()   # shallow copy 
. 7 E = copy.deepcopy (A)   # deep copy 
. 8  
. 9 b.append (. 4 )
 10 c.append (. 5 )
 . 11 d.append (. 6 )
 12 is D [. 1] .append (2 )
 13 is e.append ( . 7 )
 14 E [. 1] .append (. 3 )
 15  
16  Print (A)   # [1, [2, 2], 3, 5]
17 print(b)  # [1, [2, 2], 3, 4]
18 print(c)  # [1, [2, 2], 3, 5]
19 print(d)  # [1, [2, 2], 3, 6]
20 print(e)  # [1, [2, 3], 3, 7]

 

 4.Python line style can be doing?

  Here are some examples of Python one-liners can be seen from Python is very simple and powerful!

  1) outputs odd line of code within one hundred:

print([x for x in range(100) if x % 2])

  2) Number of daffodils line of code requirements:

print([x for x in range(100, 1000) if int(str(x)[0])**3 + int(str(x)[1])**3 + int(str(x)[2])**3 == x])

  3) multiplication table to print one line of code:

print("".join(["{} * {} = {}\t".format(x, y, x * y) if x != y else "{} * {} = {}\n".format(x, y, x * y) for x in range(1, 10) for y in range(1, x + 1)]))

  4) line of code for converting the IP address, as will be converted into 192.168.12.1 0b11000000 10101000 00001100 00000001:

print("0b"+" ".join(("00000000" + bin(int(i)).replace("0b", ""))[-8:] for i in ip.split(".")))

  5) and a line of code requirements 1 to 10:

from functools import reduce; print(reduce(lambda x, y: x + y, [i for i in range(1, 11)]))

 

 5. What is the result of the following code is?

1 def mul():
2     return [lambda x: x * i for i in range(4)]
3 
4 
5 print([m(2) for m in mul()])

  The results of the above code is output [6, 6, 6, 6], instead of [0, 2, 4, 6]!

  The reason for this problem is to delay the closure of Python bindings. This means that an internal function is called, the value of the parameter to find in the closure. So when the function mul () is called to return the value of i will look at the function returns the years, and after completion of the for loop i is 3, that is, i finally assigned to 3. Thus, each time the function returns a value that is passed by multiplying the final result, the result is that [6, 6, 6, 6].

  To solve this problem, you can refer to the following methods:

  1) using the Python generators.

1 def mul():
2     for i in range(4):
3         yield lambda x: x * i
4 
5 
6 print([m(2) for m in mul()])

  2) the creation of a closure, using the default function for binding.

1 def mul():
2     return [lambda x, i=i: x * i for i in range(4)]
3 
4 
5 print([m(2) for m in mul()])

 

Guess you like

Origin www.cnblogs.com/TM0831/p/11421384.html