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

[weekly Python 100 day question brushing plan] day5~ use of built-in functions and operators

編輯: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 < 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 !

Topic 1

 

analysis

  This topic is less difficult , Mainly investigate the use of functions and lists .

answer

def main(lst):
for i in range(len(lst)):
lst[i] = lst[i].lower()
return lst

Topic two

analysis

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 .

answer

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]

Topic three

analysis

This topic is relatively simple. It mainly focuses on the use of operators and built-in functions

answer

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

Topic four

​​​​​​​ 

analysis

This question is less difficult , Mainly investigate the use of operators and built-in functions  

answer

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 .

Topic 5

analysis

  This question mainly examines the operator , Use of anonymous and built-in functions

answer

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 .

️​​​​​​​ 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