栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

终于介绍Python 3.9

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

终于介绍Python 3.9

今天python教程栏目介绍Python 3.9。

Python 3.9,来了!

过去一年,来自世界各地的开发者们一直在致力于Python3.8的改进。Python 3.9 beta版本已经存在了一段时间,第一个正式版本于2020年10月5日发布。

每个Python版本都包含新开发和改进的功能,Python 3.9也不例外。

【python学习交流群】

下面介绍Python 3.9几个主要的新功能。

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

字典是Python中**基础的数据结构之一,并且随着python版本的迭代,性能得到不断地优化。

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

传统合并字典的方法:

>>> pycon = {2016: "Portland", 2018: "Cleveland"} # 字典1>>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"} # 字典2# 方法一>>> {**pycon, **europython}{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}#方法二>>> merged = pycon.copy>>> for key, value in europython.items:... merged[key] = value...>>> merged{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}复制代码

这两种方法都合并了字典而不更改原始数据。请注意,字典1中“Cleveland”已被合并的字典2中“Edinburgh”覆盖。

你也可以更新字典1:

>>> pycon.update(europython)>>> pycon{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}复制代码

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

>>> pycon = {2016: "Portland", 2018: "Cleveland"}>>> europython = {2017: "Rimini", 2018: "Edinburgh", 2019: "Basel"}>>> pycon | europython # 合并{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}>>> pycon |= europython # 更新>>> pycon{2016: 'Portland', 2018: 'Edinburgh', 2017: 'Rimini', 2019: 'Basel'}复制代码

d1|d2和{** d1,** d2}的作用类似,都用于合并字典取并集,遇到相同key,后者会将前者覆盖。

使用|的优势之一是它适用于类似字典的类型,并在合并后保持原来的类型:

>>> from collections import defaultdict>>> europe = defaultdict(lambda: "", {"Norway": "Oslo", "Spain": "Madrid"})>>> africa = defaultdict(lambda: "", {"Egypt": "Cairo", "Zimbabwe": "Harare"})>>> europe | africadefaultdict( at 0x7f0cb42a6700>,{'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'})>>> {**europe, **africa}{'Norway': 'Oslo', 'Spain': 'Madrid', 'Egypt': 'Cairo', 'Zimbabwe': 'Harare'}复制代码

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

>>> libraries = {... "collections": "Container datatypes",... "math": "Mathematical functions",... }>>> libraries |= {"zoneinfo": "IANA time zone support"}>>> libraries{'collections': 'Container datatypes', 'math': 'Mathematical functions','zoneinfo': 'IANA time zone support'}复制代码

|=还可以将类似字典的数据结构用于更新:

>>> libraries |= [("graphlib", "Functionality for graph-like structures")]>>> libraries{'collections': 'Container datatypes', 'math': 'Mathematical functions','zoneinfo': 'IANA time zone support','graphlib': 'Functionality for graph-like structures'}复制代码 

2. 删除字符串前缀和后缀

在Python 3.9中,可以使用.removeprefix和.removesuffix分别删除字符串的开头或结尾:

>>> "three cool features in Python".removesuffix(" Python")'three cool features in'>>> "three cool features in Python".removeprefix("three ")'cool features in Python'>>> "three cool features in Python".removeprefix("Something else")'three cool features in Python'复制代码

有人会说.strip方法也可以呀,但是该方法会出现误删操作:

>>> "three cool features in Python".strip(" Python")'ree cool features i'复制代码

可以看到,明明想删掉结尾的单词python,但是开头的there也被删除了一部分-Th。

所以.removeprefix和.removesuffix可能更精准一些。

3. zoneinfo时区模块

zoneinfo是python3.9新引入的模块,zoneinfo可以访问Internet号码分配机构(IANA)时区数据库。IANA每年都会多次更新其数据库,这是时区信息的****

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/816620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号