1. lambda函數
在Python手冊中,對labmda函數是這樣描述的。
lambda: An anonymous inline function consisting of a single expression which is evaluated when the function
is called. The syntax to create a lambda function is lambda [parameters]: expression
lambda函數是一個匿名的內置函數,其包含一個表達式且在使用這個函數的時候會進行估值計算。使用lambda函數的方式就是:
lambda 參數1, 參數2, 參數3: 表達式
f = lambda x, y : x+y
f(1,2)
# 結果為3
這里lambda作為一個表達式,定義了一個匿名函數,上例的代碼x,y為入口參數,x+y為函數體,其結果就是返回x+y的和。其實我們在使用lambda函數的時候通常是將其放在一個代碼式子中,省的單獨再去定義一個函數.
2. map函數
map()是 Python 內置的高階函數,它接收一個函數 f 和一個Iterable(必須可以由for循環的變量, 比如list, str等等),並通過把函數 f 依次作用在Iterable 的每個元素上,並把結果作為新的Iterator返回。比如:
問:如何將list列表中的字符轉換為數值型?
s = "1234567"
s = list(str[1:5]) # 這里s實字符串類型
s = map(int, s) # 將s每個字符串轉化成int類型,最后就轉化成整數數字了
這也是將list中的每個字符串元素轉換成整數了。
3. reduce函數
reduce()函數接收的參數和 map()類似,一個函數 f,一個list,但行為和 map()不同,reduce()傳入的函數 f 必須接收兩個參數,reduce()對list的每個元素反復調用函數f,並返回最終結果值。
python3使用reduce需要import一下:
from functools import reduce
print(reduce(lambda x,y:x+y,range(1,101)))
reduce()還可以接收第3個可選參數,作為計算的初始值。如果把初始值設為888,計算:
print(reduce(lambda x,y:x+y,range(1,101),888))
例如用Python求1到100的和就可以簡單的寫成:
print(reduce(lambda x,y:x+y,range(1,101)))
參考:https://blog.csdn.net/katyusha1/article/details/81538944
4.filter函數
filter():簡單的理解為過濾器,需要兩個參數,function,和一個序列(字符串、列表、元組都是序列),過濾器會依次將序列的值傳入function中,
如果返回True的話,將其重新生成一個列表返回。
list(filter(lambda x:True if x % 3 == 0 else False, range(100))) [0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
5.map函數
map():映射,用法和filter()類似,也是將序列放入函數進行運算,但是,不論運算結果為什么,map()都將忠實反饋,這是map()和filter()的主要區別。請注意,filter()和map()中的function都必要有一個返回值。
list(map(lambda x:True if x % 3 == 0 else False, range(100))) [True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True]
參考:https://www.cnblogs.com/jydeng/p/4145188.html
下面是一個運用的例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-05-19 21:08
# @Software: PyCharm
'''The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
'''
str = '73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'
from functools import reduce
delta = 13
lens = len(str)
max_num = 0
for i in range(lens - delta):
s = list(str[i: i + delta]) # 這里s實字符串類型
s = map(int, s) # 將s每個字符串轉化成int類型
a = reduce(lambda x, y: x * y, s) # 字符串里面的所有數字相乘
if a > max_num:
max_num = a
print(max_num)
結果為:23514624000