自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 3.5 帶給我們的方便的矩陣以及其他改進(jìn)

開發(fā) 后端
Python 3.5 在 2015 年首次發(fā)布,盡管它已經(jīng)發(fā)布了很長時間,但它引入的許多特性都沒有被充分利用,而且相當(dāng)酷。下面是其中的三個。

Python 3.5 在 2015 年首次發(fā)布,盡管它已經(jīng)發(fā)布了很長時間,但它引入的許多特性都沒有被充分利用,而且相當(dāng)酷。下面是其中的三個。

[[403082]]

@ 操作符

@ 操作符在 Python 中是獨(dú)一無二的,因?yàn)樵跇?biāo)準(zhǔn)庫中沒有任何對象可以實(shí)現(xiàn)它!它是為了在有矩陣的數(shù)學(xué)包中使用而添加的。

矩陣有兩個乘法的概念。元素積是用 * 運(yùn)算符完成的。但是矩陣組合(也被認(rèn)為是乘法)需要自己的符號。它是用 @ 完成的。

例如,將一個“八轉(zhuǎn)”矩陣(將軸旋轉(zhuǎn) 45 度)與自身合成,就會產(chǎn)生一個四轉(zhuǎn)矩陣。

  1. import numpy 
  2. hrt2 = 2**0.5 / 2 
  3. eighth_turn = numpy.array([ 
  4.     [hrt2, hrt2], 
  5.     [-hrt2, hrt2] 
  6. ]) 
  7. eighth_turn @ eighth_turn 
  1. array([[ 4.26642159e-17,  1.00000000e+00], 
  2.        [-1.00000000e+00, -4.26642159e-17]]) 

浮點(diǎn)數(shù)是不精確的,這比較難以看出。從結(jié)果中減去四轉(zhuǎn)矩陣,將其平方相加,然后取其平方根,這樣就比較容易檢查。

這是新運(yùn)算符的一個優(yōu)點(diǎn):特別是在復(fù)雜的公式中,代碼看起來更像基礎(chǔ)數(shù)學(xué):

  1. almost_zero = ((eighth_turn @ eighth_turn) - numpy.array([[0, 1], [-1, 0]]))**2 
  2. round(numpy.sum(almost_zero) ** 0.5, 10) 
  1. 0.0 

參數(shù)中的多個關(guān)鍵詞字典

Python 3.5 使得調(diào)用具有多個關(guān)鍵字-參數(shù)字典的函數(shù)成為可能。這意味著多個默認(rèn)值的源可以與更清晰的代碼”互操作“。

例如,這里有個可笑的關(guān)鍵字參數(shù)的函數(shù):

  1. def show_status( 
  2.     *, 
  3.     the_good=None
  4.     the_bad=None
  5.     the_ugly=None
  6.     fistful=None
  7.     dollars=None
  8.     more=None 
  9. ): 
  10.     if the_good: 
  11.         print("Good", the_good) 
  12.     if the_bad: 
  13.         print("Bad", the_bad) 
  14.     if the_ugly: 
  15.         print("Ugly", the_ugly) 
  16.     if fistful: 
  17.         print("Fist", fistful) 
  18.     if dollars: 
  19.         print("Dollars", dollars) 
  20.     if more: 
  21.         print("More", more) 

當(dāng)你在應(yīng)用中調(diào)用這個函數(shù)時,有些參數(shù)是硬編碼的:

  1. defaults = dict
  2.     the_good="You dig"
  3.     the_bad="I have to have respect"
  4.     the_ugly="Shoot, don't talk"

從配置文件中讀取更多參數(shù):

  1. import json 
  2. others = json.loads(""" 
  3. "fistful": "Get three coffins ready", 
  4. "dollars": "Remember me?", 
  5. "more": "It's a small world" 
  6. """) 

你可以從兩個源一起調(diào)用這個函數(shù),而不必構(gòu)建一個中間字典:

  1. show_status(**defaults, **others) 
  1. Good You dig 
  2. Bad I have to have respect 
  3. Ugly Shoot, don't talk 
  4. Fist Get three coffins ready 
  5. Dollars Remember me? 
  6. More It's a small world 

os.scandir

os.scandir 函數(shù)是一種新的方法來遍歷目錄內(nèi)容。它返回一個生成器,產(chǎn)生關(guān)于每個對象的豐富數(shù)據(jù)。例如,這里有一種打印目錄清單的方法,在目錄的末尾跟著 /:

  1. for entry in os.scandir(".git"): 
  2.     print(entry.name + ("/" if entry.is_dir() else "")) 
  1. refs/ 
  2. HEAD 
  3. logs/ 
  4. index 
  5. branches/ 
  6. config 
  7. objects/ 
  8. description 
  9. COMMIT_EDITMSG 
  10. info/ 
  11. hooks/ 

歡迎來到 2015 年

Python 3.5 在六年前就已經(jīng)發(fā)布了,但是在這個版本中首次出現(xiàn)的一些特性非???,而且沒有得到充分利用。如果你還沒使用,那么將他們添加到你的工具箱中。

 

責(zé)任編輯:趙寧寧 來源: Linux中國
相關(guān)推薦

2015-08-05 17:16:17

電影ip像素游戲像素大戰(zhàn)

2009-02-20 14:48:47

IT服務(wù)管理ITSM摩卡

2009-06-15 15:29:48

IT服務(wù)運(yùn)維管理摩卡

2017-11-28 16:37:19

黑五

2013-03-25 11:28:40

2010-01-26 09:20:38

Ubuntu10.04Ubuntu9.10

2009-12-07 09:12:52

2010-03-24 11:14:41

TurboLinux系

2023-01-05 08:52:42

OKR項(xiàng)目管理

2010-08-12 09:07:25

Solaris 11Oracle

2015-08-27 09:19:23

移動后端即服務(wù)MBaaSPaaS

2015-09-02 09:55:03

云服務(wù)MBaaS移動三層架構(gòu)

2011-09-26 16:23:20

桌面虛擬化虛擬化

2021-03-08 15:01:29

5G無人機(jī)光纖

2013-02-26 10:37:00

大數(shù)據(jù)云計算

2012-08-24 09:39:31

2016-12-15 14:23:49

Linux

2015-08-21 10:14:11

2023-04-03 14:32:39

異構(gòu)計算算力芯片

2020-06-10 07:40:36

CPU內(nèi)核態(tài)多線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號