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 ...
随机推荐
- 常用Oracle操作语句
--常用的字段类型有:varchar2,char,nchar,date,long,number,float,BLOB,CLOB --添加表字段 ); --修改表字段 ); --删除表字段 alter ...
- csps模拟85表达式密码,电压机制,括号密码题解
题面:https://www.cnblogs.com/Juve/articles/11733280.html 表达式密码: 是个水题... #include<iostream> #incl ...
- Linux和Windows下ping命令详解
转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] ...
- Git合并时遇到冲突或错误后取消合并
当合并分支时遇到错误或者冲突,分支旁边会多出“|MERGING”这个东西 有这个状态存在时,会导致后面想要再合并的时候提示如下 所以需要先取消这次合并,使用“git merge --abort”命令
- springcloud ribbon Finchley 版本,自定义算法
引用上一个项目,在原有的基础上进行更改,添加springcloud的内荣. eureka-server 和上一个springcloud eureka的一样,没有改动. 添加cloud-api pack ...
- [洛谷P3672]小清新签到题
题目描述 题目还是简单一点好. 给定自然数n.k.x,你要求出第k小的长度为n的逆序对对数为x的1~n的排列a1,a2...an,然后用仙人图上在线分支定界启发式带花树上下界最小费用流解决问题,保证存 ...
- MySQL数据库CRUD命令用法
数据库CRUD操作即添加(Create).读取(Read).更新(Update)和删除(Delete). 1. 添加操作也称插入操作,使用Insert语句,Insert语句可以用于几种情况: 插入完整 ...
- 在python3中的编码
在python3中的编码 #_author:Administrator#date:2019/10/29import sysprint(sys.getdefaultencoding())#utf-8 打 ...
- 并发和多线程(二)--启动和中断线程(Interrupt)的正确姿势
启动线程: 从一个最基本的面试题开始,启动线程到底是start()还是run()? Runnable runnable = () -> System.out.println(Thread.cur ...
- LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)
解题思路 明明一道比较简单的贪心结果挂了好几次23333,就是按照时间排序,然后拿一个小根堆维护放进去的,如果时间允许就入队并且记录答案.如果不允许就从堆里拿一个最小的比较. #include< ...