10 Python face questions

1, Python inside how to copy an object? (Assignment, a shallow copy and deep copy of the difference)

A: assignment (=), is to create a new object reference, modify any of the variables will affect another.

Shallow copy: create a new object, but it contains references to items comprising {1, complete slice method (modified if one of the objects, also a further modified by changing the reference) of the original object; 2, factory functions, such as list (); 3, copy module copy () function}

Deep copy: create a new object, and the object it contains recursive copy (where a modification, the other does not change) {deep.deepcopy copy module () function}

2, Python which match () and search () of the difference?

A: re module match (pattern, string [, flags]), the beginning of the string to check whether the pattern matches.

re module research (pattern, string [, flags]), first matching pattern search string in the value.

>>>print(re.match(‘super’, ‘superstition’).span())

(0, 5)

>>>print(re.match(‘super’, ‘insuperable’))

None

>>>print(re.search(‘super’, ‘superstition’).span())

(0, 5)

>>>print(re.search(‘super’, ‘insuperable’).span())

(2, 7)

3, there is no tool to help you find a bug python and static code analysis?

A: PyChecker is a python code static analysis tool, which can help find the bug python code, and warned that the complexity of the code format will

Pylint is another tool can be checked codingstandard

4, a brief description of the Python garbage collection (garbage collection).

answer

Here you can say a lot. You should mention a few key points below:

Python each object is stored in memory reference count (reference count). If the count value becomes 0, then the corresponding object will hours, the memory allocated to the object as he will be released.

Occasional reference cycle (reference cycle) will also appear. The garbage collector will regularly look for this cycle, and recovered. For example, suppose there are two objects o1 and o2, and in accordance with o1.x == o2 and o2.x == o1 these two conditions. If o1 and o2 are no other code references, then they should not continue to exist. But they are a reference count.

Python is used in some heuristics (heuristics) to speed up garbage collection. For example, the object created the later more likely to be recovered. After the object is created, the garbage collector will assign generations they belong (generation). Each object is assigned a generation younger generations are allocated object is preferentially processed.

5. What is the lambda function? What good is it?

A: lambda expressions, usually need a function, but do not want to bother to name the next occasion a function of use, but also refers to anonymous functions

lambda function: The primary purpose is short of pointing a callback function

lambda [arguments]:expression

>>> a=lambdax,y:x+y

>>> a(3,11)

6, please write some Python code that implements delete duplicate elements inside a list

answer:

1, using the set function, set (List)

2, use the dictionary function,

>>>a=[1,2,4,2,4,5,6,5,7,8,9,0]

>>> b={}

>>>b=b.fromkeys(a)

>>>c=list(b.keys())

>>> c

7, when matched with Python HTML tag, the <*> and <. *?> What is the difference?

A: The term is called greedy match (<*>) and non-greedy match (<. *?>)

E.g:

test

<.*> :

test

<.*?> :

8, how to set a global variable in a function inside?

A: The solution is to insert a global statement at the beginning of the function:

def f()

Global x

9, with the Sort program sort, then the last element from start determination

a=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3]

a.sort()

last=a[-1]

for i inrange(len(a)-2,-1,-1):

if last==a[i]:

in the [i]

else:last=a[i]

print(a)

10, what the following code in Python2 output is? Explain your answer

def div1(x,y):

print "%s/%s = %s" % (x, y, x/y)

def div2(x,y):

print "%s//%s = %s" % (x, y, x//y)

div1 (5.2)

div1 (5., 2)

Div2 (5,2)

Div2 (5, 2).

In addition, in the output of the code above Python3 What is the difference (assuming that the code in the print statement are converted into grammatical structure Python3 in)?

In Python2, the output code is:

5/2 = 2

5.0/2 = 2.5

5//2 = 2

5.0//2.0 = 2.0

By default, if the two operands are integers, the default Python 2 performs integer arithmetic. Therefore, 5/2 the result is 2, and the result was 2.5 5./2

Note that you can import the following statement to override this behavior in Python2

from __future__ import division

Also note that the "double slash" (//) operator will always perform divisible, type the number of operating ignored. This is why even result in 5.0 // 2.0 is also 2.0 Python2

But Python3 not this behavior. When two operands are integers, nor perform integer arithmetic. In Python3, the output is as follows:

5/2 = 2.5

5.0/2 = 2.5

5//2 = 2

5.0//2.0 = 2.0

Guess you like

Origin www.cnblogs.com/zaochajun/p/12034979.html
Recommended