python3 新特性
1、格式化字符串f-string
user = "Mike"
log_message = f'User{user} has logged in'
2、路径管理库Pathlib
3、类型提示Type hinting
def sentence_has_animal(sentence:str) -> bool:
return "animal" in sentence
4、枚举类enum
from enum import Enum, auto, unique
@unique #装饰器去重复
class monster(Enum):
zombie = auto()
warrior = auto()
bear = auto()
5、itertools模块
无限迭代器代码如下:
|
1
2
3
4
|
迭代器 参数 结果 例子count() start, [step] start, start+step, start+2*step, ... count(10) --> 10 11 12 13 14 ...cycle() p p0, p1, ... plast, p0, p1, ... cycle('ABCD') --> A B C D A B C D ...repeat() elem [,n] elem, elem, elem, ... endlessly or up to n times repeat(10, 3) --> 10 10 10 |
处理输入序列迭代器代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
迭代器 参数 结果 例子chain() p, q, ... p0, p1, ... plast, q0, q1, ... chain('ABC', 'DEF') --> A B C D E Fcompress() data, selectors (d[0] if s[0]), (d[1] if s[1]), ... compress('ABCDEF', [1,0,1,0,1,1]) --> A C E Fdropwhile() pred, seq seq[n], seq[n+1], starting when pred fails dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1groupby() iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)ifilter() pred, seq elements of seq where pred(elem) is True ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9ifilterfalse() pred, seq elements of seq where pred(elem) is False ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F Gimap() func, p, q, ... func(p0, q0), func(p1, q1), ... imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000starmap() func, seq func(*seq[0]), func(*seq[1]), ... starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000tee() it, n it1, it2 , ... itn splits one iterator into ntakewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4izip() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip('ABCD', 'xy') --> Ax Byizip_longest() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
组合生成器代码如下:
|
1
2
3
4
5
6
7
8
9
|
迭代器 参数 结果product() p, q, ... [repeat=1] cartesian product, equivalent to a nested for-looppermutations() p[, r] r-length tuples, all possible orderings, no repeated elementscombinations() p, r r-length tuples, in sorted order, no repeated elementscombinations_with_replacement() p, r r-length tuples, in sorted order, with repeated elementsproduct('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DDpermutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DCcombinations('ABCD', 2) AB AC AD BC BD CDcombinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD |
6、LRU缓存,memoization技术
from functools import lru_cache
@lru_cache(maxsize=512)
def fib_memoization(number:int) -> int:
if number ==0 : return 0
if number ==1 : return 1
return fib_memoization(number - 1)
start = time.time()
fib_memoization(40)
print(f'Duration:{time.time() - start}s')
7、可扩展的可迭代对象解包
head, *bodey, tail = range(5) # 0, [1,2,3], 4
8、Data class装饰器,用来减少对样板代码的使用,该装饰器会自动生成__init()__和__repr()__方法。
class Armor:
def __init__(self, armor:float, description:str, level:int = 1):
self.armor = armor
self.level = level
self.description = description def power(self) -> float:
return self.armor * self.level armor = Armor(5.2, "common armor.", 2)
armor.power() #10.4 ###使用Data Class实现相同的Armor类
from dataclasses import dataclass
@dataclass
class Armor:
armor : float
description : str
level : int = 1 def power(self) -> float:
return self.armor *self.level armor = Armor(5.2, "common armor.", 2)
armor.power()
9、bisect模块保持列表排序:
这是一个免费的二分查找实现和快速插入有序序列的工具。也就是说,你可以使用:
import bisect
bisect.insort(list, element)
python3 新特性的更多相关文章
- Python3新特性 类型注解 以及 点点点
Python3新特性 类型注解 以及 点点点 ... Python3 的新特性 Python 是一种动态语言,变量以及函数的参数是 不区分类型 的 在 函数中使用类型注解 相当于 给 形参的 类型 设 ...
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1 Python 3_x 新特性1 python3.4新特性1 python3.5新特性1 值得关注的新特性1 ...
- Python3中的新特性(3)——代码迁移与2to3
1.将代码移植到Python2.6 建议任何要将代码移植到Python3的用户首先将代码移植到Python2.6.Python2.6不仅与Python2.5向后兼容,而且支持Python3中的部分新特 ...
- 相比于python2.6,python3.0的新特性。
这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...
- python3.8 新特性
https://docs.python.org/3.8/whatsnew/3.8.html python 3.8的新功能本文解释了与3.7相比,python 3.8中的新特性. 有关完整的详细信息,请 ...
- Python3.x 常用的新特性
Python3.x 常用的新特性 print() 是函数,不是一个语句 raw_input()输入函数,改为 input() Python 3 对文本和二进制数据做了更为清晰的区分. 文本由unico ...
- python-3.8.0 新特性之赋值表达式
[python-3.8.0 新特性之赋值表达式] 赋值表达式的语法是这样的“ name := expression ”,形式上看和赋值语句 “ = ” 差不多,就作用上来看也雷同.也就是说 “:=” ...
- Ubuntu安装Python3.8及新特性
Ubuntu安装Python3.8.0a4 如果你想体验一下,请用虚拟机(感受一下就行,别当真). 新特性(整体来说,有三点特别需要注意一下) 海象运算符 # python3.7 a = '123' ...
- 你应该知道的Python3.6、3.7、3.8新特性
很多人在学习了基本的Python语言知识后,就转入应用阶段了,后期很少对语言本身的新变化.新内容进行跟踪学习和知识更新,甚至连已经发布了好几年的Python3.6的新特性都缺乏了解. 本文列举了Pyt ...
随机推荐
- XAMPP的安装及使用教程
https://blog.csdn.net/qq_36595013/article/details/80373597#3%E9%85%8D%E7%BD%AEapache
- C++如何判断某一文件是否存在
函数名: access 功 能: 确定文件的访问权限 用 法: int access(const char *filename, int amode); 程序例: #include <stdio ...
- 模块化开发(requireJS)
模块化 在前端使用模块化开发,可以将代码根据功能实施模块的划分,每个模块功能(职责)单一,在需要更改对应的功能的时候,只需要对指定的模块进行修改,其他模块不受任何影响. 为什么要进行前端模块化? 达到 ...
- selenium python bindings 元素定位
1. 辅助 Firepath Firefox是所有做前端的必不可少的浏览器因为firebug的页面元素显示很清晰.用selenium 去定位元素的时候Firefox还有一个非常友好的工具就是firep ...
- http://edu.manew.com/ ,蛮牛教育(很少免费),主要是unty3D和大数据方向。适合扫盲
http://edu.manew.com/ ,蛮牛教育(很少免费),主要是unty3D和大数据方向.
- PHP面向对象编程题(方法的实践)
<?php header('content-type:text/html;charset=utf-8'); /*设计一个peron类(有名字,年龄和蛋糕三个属性) 蛋糕一共1000块,是所有人共 ...
- Day 6:集合(set)
集合(set)定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可以作为字典的key特性:集合里面数据类型是不可变的1.可变的数据类型:列表,字典2.不可变的数据类型:数字.元组.字符串 ...
- Spring事物管理机制简单学习
首先spring并不直接管理事物,而是提供了多种事物管理器,他们将事务管理的职责委托给Hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现. Spring管理事物的接口是org.s ...
- 07_mybatis延迟加载
1. 延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: ...
- LGP5495 Dirichlet 前缀和
题目 不是很明白为什么要叫做模板 考虑到\(a_i\)能对\(b_j\)产生贡献,当且仅当\(a_i=\prod p_k^{a_k},b_j=\prod p_k^{b_k},\forall k \ a ...