Python basic interview questions (2)

16. List variable data types and immutable data types in python, and briefly describe the principles

Immutable data types: numeric, string, and tuple tuple

The value of the variable is not allowed to change. If the value of the variable is changed, it is equivalent to creating a new object. For objects with the same value, there is only one object (one address) in the memory. The id() method can be used as shown below Print object id
Insert picture description here

Variable data types: list list and dictionary dict;
the value of the variable is allowed to change, that is, if you perform operations such as append and += on the variable, only the value of the variable is changed, and a new object will not be created. The variable is referenced The address of the object will not change, but for different objects with the same value, there will be different objects in the memory, that is, each object has its own address, which is equivalent to storing multiple copies of the same value in the memory. , There is no reference count here, it is a real object.
Insert picture description here

17. s = "ajldjlajfdljfddd", de-duplicate and output "adfjl" in order from small to large

Set de-duplication, de-duplication into list, use sort method to sort, reeverse=False is to sort from small to large

List is a constant data type, there is no return value in s.sort, so the commented code is not written correctly

Insert picture description here

18. Use lambda function to multiply two numbers

Insert picture description here

19. The dictionary is sorted according to the key from small to large

dic={"name":"zs","age":18,"city":"Shenzhen","tel":"1362626627"}
Insert picture description here

20. Use the Counter method of the collections library to count the number of occurrences of each word in the string "kjalfj;ldsjafl;hdsllfdhg;lahfbl;hl;ahlf;h"

Insert picture description here

21. The string a = "not 404 found Zhang San 99 Shenzhen", with spaces in the middle of each word, filter out English and numbers with regular rules, and finally output "Zhang San Shenzhen"

Insert picture description here
By the way, paste the code that matches the decimals, although it can match, but the robustness needs to be further confirmed

Insert picture description here

22. The filter method finds all odd numbers in the list and constructs a new list, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The filter() function is used to filter the sequence, filter out elements that do not meet the conditions, and return a new list of elements that meet the conditions. It should receive two parameters, the first is a function, and the second is a sequence. Each element of the sequence is passed to the function as a parameter for judgment, and then it returns True or False, and finally the element that returns True is placed in the new list.
Insert picture description here

23. List comprehension finds all odd numbers in the list and constructs a new list, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Insert picture description here

24. What kind of data are a=(1,) b=(1), c=("1")?

Insert picture description here

25. Two lists [1,5,7,9] and [2,2,6,8] are merged into [1,2,2,3,6,7,8,9]

extend can add the elements in another set to the list one by one, which is different from append as a whole
Insert picture description here

26, database optimization query method

Foreign keys, indexes, union queries, select specific fields, etc.

27, write a custom exception code

Use raise to throw exceptions for custom exceptions
Insert picture description here

28. In regular expression matching, what is the difference between (. ) and (. ?) matching?

(. ) is a greedy match, it will match as many backwards as possible that satisfy the regularity
(.
?) is a non-greedy match, it will match as few as possible that satisfy the regularity

Insert picture description here

29. [[1,2],[3,4],[5,6]] a line of code to expand the list, get [1,2,3,4,5,6]

List comprehension

Operation process: for i in a, each i is [1,2], [3,4], [5,6], for j in i, each j is 1,2,3,4,5,6 , After merging is the result,
Insert picture description here
there is a better way to convert the list into a numpy matrix, through numpy's flatten() method, the code will always only be better, not the worst
Insert picture description here

30. x="abc",y="def",z=["d","e","f"], find the results returned by x.join(y) and x.join(z) respectively

Join() The parentheses are the iterable object, x is inserted into the iterable object to form a string, the result is the same, if you suddenly feel that the common operations of the string will not play

By the way, I suggest that you learn the os.path.join() method. The splicing path is often used and join is also used. What is the difference between join in string operations? You can consult related documents for this question, and there will be an answer later
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/107793663