程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[weekly Python 100 day question brushing plan] day4~ usage of various data types and functions and generators

編輯:Python

Catalog

️ Preface

Topic 1

analysis

answer

Topic two

analysis

answer

Topic three

analysis  

answer

Topic four

analysis

answer

Topic 5

analysis

answer

️ Last



️ Preface

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 !

Topic 1

1

analysis

Use knowledge : Various derivations in the generator , Use of functions

answer

def main(lst):
return [i for i in lst if i>=(sum(lst)/len(lst))] # This is the list derivation in the generator 


Topic two

 

 

analysis

Need to be used if sentence , Various operation methods of the list , Use of functions

answer

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 three

 

 

analysis  

Topic Too little difficulty , Mainly investigate the mastery of built-in functions

answer

def main(p,q):
return divmod(p,q) # built-in divmod Function returns the ancestor of quotient and remainder 


Topic four

analysis

  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

answer

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

  1. 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
  1. 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


Topic 5

 

analysis

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  

answer

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>

Last

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved