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 F compress() data, selectors (d[ 0 ] if s[ 0 ]), (d[ 1 ] if s[ 1 ]), ... compress( 'ABCDEF' , [ 1 , 0 , 1 , 0 , 1 , 1 ]) - - > A C E F dropwhile() pred, seq seq[n], seq[n + 1 ], starting when pred fails dropwhile( lambda x: x< 5 , [ 1 , 4 , 6 , 4 , 1 ]) - - > 6 4 1 groupby() 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 9 ifilterfalse() pred, seq elements of seq where pred(elem) is False ifilterfalse( lambda x: x % 2 , range ( 10 )) - - > 0 2 4 6 8 islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice( 'ABCDEFG' , 2 , None ) - - > C D E F G imap() func, p, q, ... func(p0, q0), func(p1, q1), ... imap( pow , ( 2 , 3 , 10 ), ( 5 , 2 , 3 )) - - > 32 9 1000 starmap() func, seq func( * seq[ 0 ]), func( * seq[ 1 ]), ... starmap( pow , [( 2 , 5 ), ( 3 , 2 ), ( 10 , 3 )]) - - > 32 9 1000 tee() it, n it1, it2 , ... itn splits one iterator into n takewhile() pred, seq seq[ 0 ], seq[ 1 ], until pred fails takewhile( lambda x: x< 5 , [ 1 , 4 , 6 , 4 , 1 ]) - - > 1 4 izip() p, q, ... (p[ 0 ], q[ 0 ]), (p[ 1 ], q[ 1 ]), ... izip( 'ABCD' , 'xy' ) - - > Ax By izip_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 - loop permutations() p[, r] r - length tuples, all possible orderings, no repeated elements combinations() p, r r - length tuples, in sorted order, no repeated elements combinations_with_replacement() p, r r - length tuples, in sorted order, with repeated elements product( 'ABCD' , repeat = 2 ) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD permutations( 'ABCD' , 2 ) AB AC AD BA BC BD CA CB CD DA DB DC combinations( 'ABCD' , 2 ) AB AC AD BC BD CD combinations_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 ...
随机推荐
- hdu6607 min25筛+杜教筛+伯努利数求k次方前缀和
推导过程类似https://www.cnblogs.com/acjiumeng/p/9742073.html 前面部分min25筛,后面部分杜教筛,预处理min25筛需要伯努利数 //#pragma ...
- 计算几何——poj1410,线段不规范交
直接用kuangbin的板子,能判不规范,规范和不交 另外线段在矩形内也可以,判断方式是比较线段的端点和矩形四个角 #include <cstdio> #include <cmath ...
- Android问题集锦An error occurred while automatically activating bundle com.android.ide.eclipse.adt .
这是Eclipse的问题,重启之后问题依旧. 在坛子里找到这个办法,果然好用. 命令行到eclipse路径运行:eclipse.exe -clean
- PHP实现对短信验证码发送次数的限制
http://www.jb51.net/article/94878.htm 对用户获取短信验证码的手机号.ip.和浏览器(使用唯一标识)进行限制.本文介绍的方法是对用户每天只能通过同一浏览器或同一ip ...
- 372 在O(1)时间复杂度删除链表节点
原题网址:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/ 给定一个单链表中 ...
- Java中String str=new String("a")和String str = "a"有什么区别?
Q: String A="ABC"; String B=new String("ABC"); 这两个值,A,B 是否相等,如果都往HashSet里面放,能放下吗 ...
- 初识OpenCV-Python - 002: Drawing functions
使用OpenCV-Python 的画图函数画图. 本次的图形函数有: cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), cv2.put ...
- 【AT3611】Tree MST
题目 这个题的输入首先就是一棵树,我们考虑一下点分 我们对于每一个分治重心考虑一下跨过这个分治重心的连边情况 就是把当前分治区域内所有的点向距离分治重心最近的点连边 考虑一下这个算法的正确性,如果我们 ...
- hadoop 环境下不知道yarn端口可以通过此命令查找
yarn jar hadoop-examples-2.6.0-mr1-cdh5.10.0.jar pi 1 30 hadoop-examples-2.6.0-mr1-cdh5.10.0.jar 此JA ...
- swagger请求参数在header中添加token
网友大部分说的是如下配置 参照配置然而没有作用 注掉改红框内的配置,在方法上加如下注释就可以用 @ApiImplicitParams({ @ApiImplicitParam(paramType = & ...