Catalog
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 < Use of built-in functions and operators >, Take you to master Python Basics , I hope you can gain more knowledge here ! Let's learn together ! Progress together !
This topic is less difficult , Mainly investigate the use of functions and lists .
def main(lst): for i in range(len(lst)): lst[i] = lst[i].lower() return lst
The difficulty of this problem is medium , Mainly investigate the use of operators and built-in functions , It's easy to think about which built-in function to use .
def main(lst): return sorted(lst,key=len,reverse=Ture)
About built-in functions sorted() Just look at the following code , The notes are very detailed
l1 = ['3www','4wwww','2ww','5wwwww'] # No, reverse=True By default, they are sorted from small to large l2 = sorted(l1,key=len) print(l2) # Yes reverse=True It is sorted from big to small l3 = sorted(l1,key= len,reverse=True) print(l3) # First of all, no reverse=True, List l4 Each value in i Conduct abs(i), # Then sort the changed values in descending order 22<77<333 l4 = [22,-333,77] l5 = sorted(l4,key=abs) # Yes reverse=True List l4 Each value in i Conduct abs(i), # Then sort the changed values in descending order 333>77>22 print(l5) l6 = sorted(l4,key= abs,reverse=True) print(l6) Output results : ['2ww', '3www', '4wwww', '5wwwww'] ['5wwwww', '4wwww', '3www', '2ww'] [22, 77, -333] [-333, 77, 22]
This topic is relatively simple. It mainly focuses on the use of operators and built-in functions
from operator import mul def main(vector1,vector2): res = 0 for i in range(len(vector1)): s = mul(vector1[i],vector2[i]) res += s return res
This question is less difficult , Mainly investigate the use of operators and built-in functions
def main(lst): return sorted(lst,key=len)[len(lst)-1]
Start with the list sorted(lst,key=len) The operation of , It becomes a new list sorted by element string from small to large , So the longest string in the list is at the end of the new list , After that, I was using it ( New list [len(lst)-1]) Get the last element , You get the longest string .
This question mainly examines the operator , Use of anonymous and built-in functions
def main(lst): return list(filter(lambda n:n!=0,lst)
Pass in the values of the original list one by one lambda n:n!=0 Anonymous function , If True Return to the new list .
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