python高階技能
1、匿名函式:lambda¶
In [1]:
f=lambda x,y,z:x+y+z
f(2,3,4)
Out[1]:
9
In [2]:
L=[lambda x:x**2,
lambda x: x**3,
lambda x:x**4]
for f in L:
print(f(2))
4
8
16
In [3]:
import sys
showall=lambda x:[sys.stdout.write(line) for line in x] #在lambda中進行print
t=showall(('bright\n', 'side\n', 'of\n', 'life\n'))
bright
side
of
life
2、在序列重對映函式:map¶
In [4]:
counters=[1,2,3,4]
def inc(x):return x+10
map(inc,counters)
#說明:map函式對一個序列物件中的每一個元素應用被傳入的函式,並且返回一個包含了所有函式呼叫結果的一個列表
Out[4]:
[11, 12, 13, 14]
In [5]:
pow(2,3)
Out[5]:
8
In [6]:
map(pow,[1,2,3],[2,3,4])
Out[6]:
[1, 8, 81]
3、函數語言程式設計工具:filter和reduce¶
In [7]:
range(-5,5)
Out[7]:
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
In [8]:
filter((lambda x:x>0), range(-5,5))
Out[8]:
[1, 2, 3, 4]
In [9]:
reduce((lambda x,y:x+y),[1,2,3,4])
Out[9]:
10
In [10]:
reduce((lambda x,y:x*y),[1,2,3,4])
Out[10]:
24
In [11]:
reduce((lambda x,y:x*y),[1,2,3,4])
Out[11]:
24
In [ ]:
4、python3的reduce及如何檢視python版本資訊¶
In [1]:
import platform
In [2]:
platform.python_version()
Out[2]:
'3.5.2'
In [3]:
from functools import reduce
In [4]:
reduce((lambda x,y:x+y),[1,2,3,4])
Out[4]:
10
In [5]:
import sys
print(sys.version)
3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
In [1]:
f=lambda x,y,z:x+y+z
f(2,3,4)
Out[1]:
9
In [2]:
L=[lambda x:x**2,
lambda x: x**3,
lambda x:x**4]
for f in L:
print(f(2))
4
8
16
In [3]:
import sys
showall=lambda x:[sys.stdout.write(line) for line in x] #在lambda中進行print
t=showall(('bright\n', 'side\n', 'of\n', 'life\n'))
bright
side
of
life
2、在序列重對映函式:map¶
In [4]:
counters=[1,2,3,4]
def inc(x):return x+10
map(inc,counters)
#說明:map函式對一個序列物件中的每一個元素應用被傳入的函式,並且返回一個包含了所有函式呼叫結果的一個列表
Out[4]:
[11, 12, 13, 14]
In [5]:
pow(2,3)
Out[5]:
8
In [6]:
map(pow,[1,2,3],[2,3,4])
Out[6]:
[1, 8, 81]
3、函數語言程式設計工具:filter和reduce¶
In [7]:
range(-5,5)
Out[7]:
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
In [8]:
filter((lambda x:x>0), range(-5,5))
Out[8]:
[1, 2, 3, 4]
In [9]:
reduce((lambda x,y:x+y),[1,2,3,4])
Out[9]:
10
In [10]:
reduce((lambda x,y:x*y),[1,2,3,4])
Out[10]:
24
In [11]:
reduce((lambda x,y:x*y),[1,2,3,4])
Out[11]:
24
In [ ]:
4、python3的reduce及如何檢視python版本資訊¶
In [1]:
import platform
In [2]:
platform.python_version()
Out[2]:
'3.5.2'
In [3]:
from functools import reduce
In [4]:
reduce((lambda x,y:x+y),[1,2,3,4])
Out[4]:
10
In [5]:
import sys
print(sys.version)
3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
相關文章
- 值得了解的webpack高階技能Web
- Python 高階特性Python
- 提升開發技能:10個高階的JavaScript技巧JavaScript
- 提升編碼技能的 幾 種高階技術
- Python模組高階技巧Python
- python高階函式Python函式
- python高階特性-sorted()Python
- python高階特性-迭代Python
- Python 高階函式Python函式
- Python高階--閉包Python
- python中list列表的高階應用 高階函式Python函式
- Python 函式進階-高階函式Python函式
- 高階軟體測試工程師,需要掌握哪些技能工程師
- python- 函式高階Python函式
- python高階之函式Python函式
- Python——迭代器的高階用法Python
- 02 . SaltStack高階用法(Python API)PythonAPI
- Python裝飾器高階用法Python
- python是高階語言嗎Python
- Python高階程式設計技巧Python程式設計
- python str.format高階用法PythonORM
- Python進階:切片的誤區與高階用法Python
- Python Django進階教程(三)(模型的高階用法)PythonDjango模型
- Python高階 -- 08 MySQL與Python互動PythonMySql
- 高階PHP工程師應該具備的一些技能PHP工程師
- 高階Linux運維工程師必備技能(掃盲篇)Linux運維工程師
- 高階PHP工程師所應該具備一些技能PHP工程師
- python高階-閉包-裝飾器Python
- Python中函式的高階使用Python函式
- python高階語法:繼承性Python繼承
- Python 高階特性(4)- 生成器Python
- Python高階 -- 07 MySQL資料庫PythonMySql資料庫
- 初學 Python(十二)——高階函式Python函式
- Python類繼承的高階特性Python繼承
- Python高階特性(3): Classes和MetaclassesPython
- 快速掌握awk linux#高階程式設計師必備技能#薦Linux程式設計師
- Python高階 -- 09 MySQL高階之事務、檢視、索引、賬戶管理、主從配置PythonMySql索引
- 從高階函式--->高階元件函式元件