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

Python learning notes: anonymous functions

編輯:Python
# Anonymous functions omit function names and return value statements , effect :
# Anonymous function writing
# lambda Understood as a way to quickly locate functions
square = lambda n: n * n
# notes : there squre Is a function type variable
# Function definition writing
def square1(n):
return n*n
print(square(3))
print(square1(3))
# Note that the above two expressions are equivalent
# Example
revenue = [('1 month ', 100), ('2 month ', 200), ('3 month ', 300)]
key = lambda x: x[1] # key Is to return the second element
for r in revenue:
print(key(r))
# Example
plus = lambda a, b: a + b
print(plus(3, 5))
print(plus('x', 'y'))
# Example
revenue1 = [('1 quarter ', (100, 200, 300)),
('2 quarter ', (200, 500, 600)),
('3 quarter ', (300, 400, 700))]
# Add up the turnover of each quarter
# revenue1.sort(key=lambda x:x[1][0] + x[1][1] + x[1][2])
# perhaps
revenue1.sort(reverse=True, key=lambda x:sum(x[1])) #reverse=True, Reverse sort
print(revenue1)


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