成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python 3.9,來了!

開發 前端
Python 3.9,來了!每個Python版本都包含新開發和改進的功能,Python 3.9也不例外。

Python 3.9,來了!

過去一年,來自世界各地的開發者們一直在致力于Python3.8的改進。Python 3.9 beta版本已經存在了一段時間,第一個正式版本于2020年10月5日發布。

每個Python版本都包含新開發和改進的功能,Python 3.9也不例外。

下面介紹Python 3.9幾個主要的新功能。

1. 字典(合并&更新)運算符

字典是Python中最基礎的數據結構之一,并且隨著python版本的迭代,性能得到不斷地優化。

Python3.9中,合并(|)和更新(|=)運算符已添加到dict類中。這些更新完善了現有的dict.update和{** d1,** d2}方法。

傳統合并字典的方法:

  1. >>> pycon = {2016: "Portland", 2018: "Cleveland"} # 字典1 
  2. >>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} # 字典2 
  3.  
  4. # 方法一 
  5. >>> {**pycon, **europython} 
  6. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 
  7.  
  8. #方法二 
  9. >>> merged = pycon.copy() 
  10. >>> for key, value in europython.items(): 
  11. ...     merged[key] = value 
  12. ... 
  13. >>> merged 
  14. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 

這兩種方法都合并了字典而不更改原始數據。請注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆蓋。

你也可以更新字典1:

  1. >>> pycon.update(europython) 
  2. >>> pycon 
  3. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 

新版本的Python引入了兩個新的字典運算符:合并(|)和更新(|=)。你可以使用|合并兩個字典,而|=用于更新字典:

  1. >>> pycon = {2016: "Portland", 2018: "Cleveland"} 
  2. >>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} 
  3.  
  4. >>> pycon | europython  # 合并 
  5. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 
  6.  
  7. >>> pycon |= europython # 更新 
  8. >>> pycon 
  9. {2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'} 

d1|d2和{** d1,** d2}的作用類似,都用于合并字典取并集,遇到相同key,后者會將前者覆蓋。

使用|的優勢之一是它適用于類似字典的類型,并在合并后保持原來的類型:

  1. >>> from collections import defaultdict 
  2. >>> europe = defaultdict(lambda: "", {"Norway": "Oslo", "Spain": "Madrid"}) 
  3. >>> africa = defaultdict(lambda: "", {"Egypt": "Cairo", "Zimbabwe": "Harare"}) 
  4.  
  5. >>> europe | africa 
  6. defaultdict(<function <lambda> at 0x7f0cb42a6700>
  7.   {'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'}) 
  8.  
  9. >>> {**europe, **africa} 
  10. {'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'} 

|=的作用是更新字典,類似于.update():

  1. >>> libraries = { 
  2. ...     "collections": "Container datatypes", 
  3. ...     "math": "Mathematical functions", 
  4. ... } 
  5. >>> libraries |= {"zoneinfo": "IANA time zone support"} 
  6. >>> libraries 
  7. {'collections': 'Container datatypes', 'math': 'Mathematical functions', 
  8.  'zoneinfo': 'IANA time zone support'} 

|=還可以將類似字典的數據結構用于更新:

  1. >>> libraries |= [("graphlib", "Functionality for graph-like structures")] 
  2. >>> libraries 
  3. {'collections': 'Container datatypes', 'math': 'Mathematical functions', 
  4.  'zoneinfo': 'IANA time zone support', 
  5.  'graphlib': 'Functionality for graph-like structures'} 

2. 刪除字符串前綴和后綴

在Python 3.9中,可以使用.removeprefix()和.removesuffix()分別刪除字符串的開頭或結尾:

  1. >>> "three cool features in Python".removesuffix(" Python") 
  2. 'three cool features in' 
  3.  
  4. >>> "three cool features in Python".removeprefix("three ") 
  5. 'cool features in Python' 
  6.  
  7. >>> "three cool features in Python".removeprefix("Something else") 
  8. 'three cool features in Python' 

有人會說.strip方法也可以呀,但是該方法會出現誤刪操作:

  1. >>> "three cool features in Python".strip(" Python") 
  2. 'ree cool features i' 

可以看到,明明想刪掉結尾的單詞python,但是開頭的there也被刪除了一部分-Th。

所以.removeprefix()和.removesuffix()可能更精準一些。

3. zoneinfo時區模塊

zoneinfo是python3.9新引入的模塊,zoneinfo可以訪問Internet號碼分配機構(IANA)時區數據庫。IANA每年都會多次更新其數據庫,這是時區信息的最權威來源。

使用zoneinfo,可以獲得數據庫中描述任何時區的對象:

  1. >>> from zoneinfo import ZoneInfo 
  2. >>> ZoneInfo("America/Vancouver") 
  3. zoneinfo.ZoneInfo(key='America/Vancouver'
  1. >>> from zoneinfo import ZoneInfo 
  2. >>> from datetime import datetime, timedelta 
  3.  
  4. >>> # 夏令時 
  5. >>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles")) 
  6. >>> print(dt) 
  7. 2020-10-31 12:00:00-07:00 
  8. >>> dt.tzname() 
  9. 'PDT' 
  10.  
  11. >>> # 標準時間 
  12. >>> dt += timedelta(days=7
  13. >>> print(dt) 
  14. 2020-11-07 12:00:00-08:00 
  15. >>> print(dt.tzname()) 
  16. PST 

4. 內置集合類型用于類型提示

在類型提示中,現在可以將內置集合類型(例如list和dict)用作泛型類型,而不必從typing中導入相應的大寫類型(例如List或Dict)。

  1. def greet_all(names: list[str]) -> None: 
  2.     for name in names: 
  3.         print("Hello", name) 

5. 拓撲排序

Python 3.9添加了一個新的模塊graphlib,其中包含graphlib.TopologicalSorter類,以提供執行拓撲排序的功能。

  1. >>> dependencies = { 
  2. ...     "realpython-reader": {"feedparser", "html2text"}, 
  3. ...     "feedparser": {"sgmllib3k"}, 
  4. ... } 
  5. ... 
  6.  
  7. >>> from graphlib import TopologicalSorter 
  8. >>> ts = TopologicalSorter(dependencies) 
  9. >>> list(ts.static_order()) 
  10. ['html2text', 'sgmllib3k', 'feedparser', 'realpython-reader'] 

6. 最小公倍數(LCM)

Python長期以來一直具有用于計算兩個數字的最大公約數(GCD)的功能:

  1. >>> import math 
  2. >>> math.gcd(49, 14) 

最小公倍數(LCM)與最大公約數(GCD)有關,可以根據GCD定義LCM:

  1. >>> def lcm(num1, num2): 
  2. ...     if num1 == num2 == 0: 
  3. ...         return 0 
  4. ...     return num1 * num2 // math.gcd(num1, num2) 
  5. ... 
  6. >>> lcm(49, 14) 
  7. 98 

在Python 3.9中,不再需要定義自己的LCM函數,它新增了計算最小公倍數功能:

  1. >>> import math 
  2. >>> math.lcm(49, 14) 
  3. 98 

7. 更強大的Python解析器

Python 3.9最酷的功能之一是大家在日常編程中不會注意到的功能,那就是解析器的更新。解析器是Python解釋器的基本組件。在最新版本中,解析器已重新構建。

Python之前一直使用LL(1)解析器將源代碼解析為解析樹。你可以將LL(1)解析器視為一次讀取一個字符,并解釋源代碼而無需回溯的解析器。

新解釋器是基于PEG(parsing expression grammar)實現的,并非LL(1)。新解析器的性能可以與舊解析器媲美,在設計新語言功能時,PEG比LL(1)更靈活。

在整個標準庫中,PEG解析器稍快一些,然而也使用了更多的內存。實際上,使用新解析器時,很難能感知到性能的好壞。

 

責任編輯:趙寧寧 來源: Python大數據分析
相關推薦

2020-10-14 15:00:38

Python 開發編程語言

2021-01-04 08:15:16

CentOS 7Python3.9Python

2020-04-27 15:43:45

Python 3.9python開發

2021-06-14 09:25:20

PythonPython 3.9編程語言

2021-01-20 23:53:16

PythonPython 3.9開發

2013-03-05 09:35:54

Linux

2021-04-16 16:21:02

鴻蒙HarmonyOS應用開發

2025-01-15 10:02:09

APIVueDOM

2019-11-25 15:58:15

Python 3.9新特性模塊

2010-02-03 13:25:34

云計算

2024-05-08 08:50:39

React19模式UI

2021-05-26 16:10:00

Python 開發編程語言

2022-11-29 07:48:16

2012-05-22 01:31:30

Highlight代碼工具Java

2020-09-28 07:56:16

Python3.9Python開發

2020-10-31 21:50:54

Python3.9Python開發

2023-04-23 18:56:53

LinuxKubuntu

2014-04-17 11:45:39

Ultimate EdLinux 發行版

2014-06-23 09:04:56

Docker

2014-12-08 09:55:33

Android 5.0Google
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品国产欧美一区二区成人 | 久久四虎 | 成人激情免费视频 | 欧美性极品xxxx做受 | 亚洲欧美视频 | 欧美在线视频一区二区 | 欧美高清一区 | 国产日韩免费视频 | 久久精品在线免费视频 | 欧美日韩大陆 | 99色综合| 欧美精品一区二区三区蜜桃视频 | 国产日韩欧美一区二区 | 国产99热精品 | 亚洲精品99 | 九七午夜剧场福利写真 | 九七午夜剧场福利写真 | 伊人最新网址 | 欧美国产日本一区 | 久久电影一区 | 国产精品毛片久久久久久久 | 91精品国产欧美一区二区成人 | 免费人成在线观看网站 | 天天干夜夜操 | 国产日韩欧美 | 国产在线播| 欧美日韩看片 | 亚洲欧洲一区二区 | 成人高清视频在线观看 | 在线观看成年人视频 | 国产人久久人人人人爽 | 91成人免费观看 | 在线2区 | 99视频免费 | ririsao久久精品一区 | 久久久久国产精品 | 夜夜夜久久久 | 热99精品视频 | 99久久免费精品国产男女高不卡 | 久久国产婷婷国产香蕉 | 亚洲国产欧美在线人成 |