无穷嵌套的列表
>>> a = [1, 2, 3, 4] >>> a.append(a) >>> a [1, 2, 3, 4, [...]] >>> a[4] [1, 2, 3, 4, [...]] >>> a[4][4][4][4][4][4][4][4][4][4] == a True
无穷嵌套的字典
>>> a = {} >>> b = {} >>> a['b'] = b >>> b['a'] = a >>> print a {'b': {'a': {...}}}
列表重构
>>> l = [[1, 2, 3], [4, 5], [6], [7, 8, 9]] >>> sum(l, []) [1, 2, 3, 4, 5, 6, 7, 8, 9]
或者
import itertools data = [[1, 2, 3], [4, 5, 6]] list(itertools.chain.from_iterable(data))
再或者
from functools import reduce from operator import add data = [[1, 2, 3], [4, 5, 6]] reduce(add, data)
字典生成
>>> {a:a**2 for a in range(1, 10)} {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
类中方法重置
class foo:
def normal_call(self):
print("normal_call")
def call(self):
print("first_call")
self.call = self.normal_call
>>> y = foo()
>>> y.call() first_call >>> y.call() normal_call >>> y.call() normal_call
获取类属性
class GetAttr(object):
def __getattribute__(self, name):
f = lambda: "Hello {}".format(name) return f>>> g = GetAttr() >>> g.Mark()
'Hello Mark'
集合
>>> a = set([1,2,3,4]) >>> b = set([3,4,5,6]) >>> a | b # Combining{1, 2, 3, 4, 5, 6} >>> a & b # Intersection{3, 4} >>> a < b # SubsetsFalse>>> a - b # Variance{1, 2} >>> a ^ b # The symmetric difference{1, 2, 5, 6}
集合定义必须使用set关键字, 除非使用集合生成器
{ x for x in range(10)} # Generator sets
set([1, 2, 3]) == {1, 2, 3} set((i*2 for i in range(10))) == {i*2 for i in range(10)}
比较操作
>>> x = 5
>>> 1 < x < 10
True
>>> 10 < x < 20
False
>>> x < 10 < x*10 < 100
True
>>> 10 > x <= 9
True
>>> 5 == x > 4
True
动态创建新类
>>> NewType = type("NewType", (object,), {"x": "hello"}) >>> n = NewType() >>> n.x'hello'
另一个普通版本
>>> class NewType(object): >>> x = "hello" >>> n = NewType() >>> n.x"hello"
条件赋值
x = 1 if (y == 10) else 2
x = 3 if (y == 1) else 2 if (y == -1) else 1
变量解包
>>> first,second,*rest = (1,2,3,4,5,6,7,8) >>> first # The first value1
>>> second # The second value2
>>> rest # All other values
[3, 4, 5, 6, 7, 8]>>> first,*rest,last = (1,2,3,4,5,6,7,8) >>> first1>>> rest [2, 3, 4, 5, 6, 7] >>> last8
列表元素的序号
>>> l = ["spam", "ham", "eggs"] >>> list(enumerate(l)) >>> [(0, "spam"), (1, "ham"), (2, "eggs")] >>> list(enumerate(l, 1)) # 指定计数起点
>>> [(1, "spam"), (2, "ham"), (3, "eggs")]
异常捕获中使用else
try: function()
except Error: # If not load the try and declared Error
else: # If load the try and did not load except
finally: # Performed anyway
列表拷贝
错误的做法
>>> x = [1, 2, 3] >>> y = x >>> y[2] = 5>>> y [1, 2, 5] >>> x [1, 2, 5]
正确的做法
>>> x = [1,2,3] >>> y = x[:] >>> y.pop()3>>> y [1, 2] >>> x [1, 2, 3]
对于递归列表的做法
import copy my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]} my_copy_dict = copy.deepcopy(my_dict)
英文原文: https://www.devbattles.com/en/sand/post-1799-Python_the_things_that_you_might_not_know
译者: 诗书塞外
相关推荐
Python实用教程:Python基础,Python高级特性,面向对象编程,多线程,数据库,数据科学,Flask,爬虫开发教程。内部含有学习笔记、MD文档、项目教程、笔记文档 Python实用教程:Python基础,Python高级特性,面向...
压缩包中的图是根据实际采集到的发动机数据(出于保密,无法上传),采用多元线性回归拟合绘制的图。其中多元线性回归主要是调用sklearn库来完成,绘图部分主要调用matplotlib。因为是自己看函数文档,一点一点手写...
python 学习资源包包括python核心编程第二版/dive into python/python3新特性等文档资料
主要是用来学习和了解python语言的特性,对于python语言有更深的认识。
根据自学总结的Python入门经验(列表特性),方便刚入门Python的同学加深理解列表的基础。
文档对Python语言提供的特殊方法进行了梳理,基于特性访问、可调用对象、集合、数字、上下文和迭代器做分类。
Python 中许多最引人瞩目的特性,并且会给你一个关于语言特色和风格的认识。读完之后,你将能够阅读 和编写 Python 模块或程序,并为以后使用 Python 参考手册 继续学习诸多 Python 模块库做好准备。
网络上能找到的所有python技巧,都在这里了。尤其是最新的python3的新特性新技巧
Python3.5中async_await特性的实现
讲述了python面型对象,对类属性的操作,垃圾回收,类的继承,方法重写以及python json等
Python3.5中async_await特性的实现.pdf
作者巴里觉得你的时间相当宝贵,不应当过多地花费在与新概念的纠缠之中。通过应用认知科学和学习理论的最新研究成果,《Head First Python(中文版)》可以让你投入一个需要多感官参与的学习体验,这本书采用丰富直观...
相反地,它介绍了 Python 中许多最引人瞩目的特性,并且会给你一个关于语言特色和风格的认识。读完之后,你将能够阅读和编写 Python 模块或程序,并为以后使用 Python 参考手册 继续学习诸多 Python 模块库做好准备...
Python实用教程,包括:Python基础,Python高级特性,面向对象编程-python-tutorial
python中文编程第二版,一本有关python开发的权威指南,涵盖了近期发布的python版本的核心功能特性而不仅仅是语法。
深入理解python 快速掌握python指南 python笔记 web框架Django开发
这篇文档介绍了BeautifulSoup4中所有主要特性,并且有小例子.让我来向你展示它适合做什么,如何工作,怎样使用,如何达到你想要的效果,和处理异常情况. 文档中出现的例子在Python2.7和Python3.2中的执行结果相同 你...
更新以反映Python编程范例中的最新和Python 3中发现的⼏个最关键特性,开始Python还包括⾼级主题,如扩展Python和打包/分发 Python应⽤程序。 你会学到什么 跟随⼀个友好的,实⽤的语⾔关键特性指南,成为精通...
Python语言在PN结特性和玻尔兹曼常数测定实验数据处理中的应用.pdf