Topic 1
analysis
answer
Topic two
analysis
answer
Topic three
analysis
answer
Topic four
analysis
answer
Topic 5
analysis
answer
The topic of this time is from Mr. dongfuguo Python Programming questions It mainly introduces to you Python Classic example < Usage of various data types and functions and generators >, Take you to master Python Basics , I hope you can gain more knowledge here ! Let's learn together ! Progress together !
1
Use knowledge : Various derivations in the generator , Use of functions
def main(lst): return [i for i in lst if i>=(sum(lst)/len(lst))] # This is the list derivation in the generator
Need to be used if sentence , Various operation methods of the list , Use of functions
def main(lst,item): if item in lst: # First determine whether this number exists in the list return lst.index(item) # In the list index Use of methods else: return ' non-existent '
Topic Too little difficulty , Mainly investigate the mastery of built-in functions
def main(p,q): return divmod(p,q) # built-in divmod Function returns the ancestor of quotient and remainder
This topic is a bit difficult to think of , The main method used is the use of built-in functions , As for which one it is, you have to think for yourself
def main(num): return sum(map(int,str(num)))
Let's start with why str(num):
from map() Function syntax :map(function, iterable, ...), Because an integer was received num Is not iterative , and map Syntax should be iteratable, so using data type cast will num It can be converted to an iteratable string data type map The use condition of the function .
The following code is easy to understand , Now let's talk about it step by step
- Let's start with the internal map(int,str(num)) The purpose of this step
a = 1234567 print(map(int,str(a))) for i in map(int,str(a)): print(i) Output results : <map object at 0x000001FEB8AAF0D0> 1 2 3 4 5 6 7
- Let's talk about the outer layer sum function ,sum The function syntax is sum(*args, **kwargs),sum Receive the dynamic parameters passed in the first step and add them to get the result
The fifth question is also the most difficult one among these questions , It tests our built-in functions , Use of operators , A lot of restrictions have been added
First, let's demonstrate what I started with Wrong answer , The display on this software is wrong
But in Python The code that runs in is like this
The reason for the lack of rigor is that it translates into set(lst) Although the weight has been removed , But the resulting set is unordered , After the list(set(lst)) It can also be disordered , This does not match the order required by the meaning of the question
Correct answer
Yes, in the answer lst2.sort(key=lst.index) Explain , The notes are very detailed .
li = [1,2,3] print(li.index) #print(li.index), The output string is the function name ( The address of the function in memory ) # if b It's also a list ,b.sort(key=a.index), # among key Is a formal parameter , Receive type must be function . # The list of b Every element in the must go through a.index Function mapping (y=f(x)), # Sort by mapping value size # Generally speaking, carry on b.sort(key=a.index) after b Will press a The order of the list . Output results : <built-in method index of list object at 0x0000026A26484B00>
Thank you for seeing here : In an unpublished article, Lu Xun said :“ If you understand the code, you must operate it yourself, so that you can better understand and absorb .”
One last sentence : A man can succeed in anything he has unlimited enthusiasm , Let's make progress together