One. How is python managed in memory?
Python introduces a mechanism: reference counting.
Python internally uses reference counting to keep track of objects in memory, and Python internally records how many references the object has, that is, the reference count, when the object is created, a reference count is created, and when the object is no longer needed, the object has a reference count of 0 o'clock, which is garbage collected.
Summing up the object will reference the Count plus 1 in the following cases:
1. Object created: x=4
2. Other people were created: y=x
3. Passed as a parameter to the function: Foo (x)
4. As an element of the container object: a=[1,x, ' 33 ']
Reference count Reduction Scenario
1. A local reference has left its scope. For example, at the end of the foo (x) function above, X points to the object reference minus 1.
2. The alias of the object is explicitly destroyed: Del x; or del y
3. An alias of an object is assigned to another object: x=789
4. Object is removed from a Window object: Mylist.remove (x)
5. The Window object itself is destroyed: Del myList, or the Window object itself is out of scope.
Garbage collection
1. When there are no longer parts in memory, the garbage collector will clean them off. It checks for those objects that have a reference count of 0, and then clears its memory space. Of course, in addition to the reference count of 0 will be cleared, there is also a situation will be cleared by the garbage collector: when two objects refer to each other, their own other references are already 0.
2. Garbage collection mechanism There is also a recycle garbage collector, which ensures that the circular reference object is released (a reference B, B refers to a, which causes its reference count to never be 0).
In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory.
Memory pool mechanism
Python provides a garbage collection mechanism for memory, but it puts unused memory into the memory pool instead of returning it to the operating system.
All objects less than 256 bytes in Python Use the allocator implemented by Pymalloc, while large objects use the system malloc. In addition, Python objects, such as integers, floating-point numbers, and lists, have their own private pools of memory that do not share their pools of memory among objects. That is, if you allocate and release a large number of integers, the memory used to cache these integers can no longer be assigned to floating-point numbers.
In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory. This is the pymalloc mechanism mentioned earlier.
Two. Go to heavy a=[1,2,4,2,4,5,6,5,7,8,9,0]
Method One
A = [1,2,3,3,4,2,3,4,5,6,1]
News_a = []
For ID in a:
If ID not in news_a:
News_a.append (ID)
Print (news_a)
Method Two
ids = [1,4,3,3,4,2,3,4,5,6,1]
News_ids = List (set (IDS))
News_ids.sort (Ids.index)
Three. What is a lambda function? What good is it?
What is the LAMDA function?
A lambda function is a function that can receive any number of arguments (including optional arguments) and return a single expression value. (Note: Lambda functions cannot contain commands, they contain no more than one expression)
What are the benefits of the LAMDA function?
1, lambda function is relatively light, that is, still, it is suitable for the need to complete a function, but this function is only used in this one place, even the name is very casual circumstances;
2, anonymous function, generally used to give filter,map such a function-type programming services;
3, as a callback function, passed to some applications, such as message processing
Personal Understanding:
1. No need to define function name (temporary function)
2. Only simple operation, and return value, no complex function body
Four. Describe the use of the range () function under Python?
If you need to iterate over a sequence of numbers, you can use the range () function, and the range () function can generate arithmetical progression.
As an example:
For I in range (5):
Print (i)
This code will output, 1, 2, 3, 45 digits
A range (1) produces 1 values, or you can have range () start with another number, or define a different increment, or even a negative increment
Range (1, 10, 3) increments to three, return value is List [1,4,7]
The range (1,5) increment is the default of 1, and the return value is the list, which is [1,2,3,4,5]
Range () and Len () can also be used together to iterate over an index sequence
For example:
A = [' Nina ', ' Jim ', ' Rainman ', ' Hello ']
For I in range (Len (a)):
Print (I, a[i])
The return value is:
(0, ' Nina ')
(1, ' Jim ')
(2, ' Rainan ')
(3, ' Hello ')
Five. What is the difference between,<.*> and <.*?> when matching HTML tags with python?
When a regular expression is repeatedly matched, such as <.*>, the maximum matching value is returned when the program executes the match.
For example:
Import re
s = ' Print (Re.match (' <.*> ', s). Group ())
will return a match and
Import re
s = ' Print (Re.match (' <.*?> ', s). Group ())
will return to the <.*> This match is called greedy match <.*?> called non-greedy match
Python face question