Fluent Python简记

Fluent Python 笔记
一、序列(Sequence)
1、容器序列(Container sequences):list, tuple, collections.deque ...
2、平坦序列(Flat sequences):str, bytes, bytearray, memoryview, array.array

根据可不可变分类:
1)可变的序列:list, bytearray, array.array, collections.deque, memoryview
2)不可变序列:tuple, str, bytes

二、如何快速生成一个序列
1)列表推导(List Comprehensions)
2)生成器表达式(Generator Expressions)

推荐列表推导:
words = ['The','quick','BROWN','Fox','jumed','OVER','the','Lazy','DOG']
[word for word in words if word.islower()]
===========================================
['quick', 'jumed', 'the']

列表推导格式以 [] 为标志,内容分为三部分 [A B C],A,B,C分别代表一个表达式,其中C可以省略。
对于上面的例子:
A:word
B:for word in words
C:if word.islower()

首先看B,B的格式一般为 for x in xxx,表示从一个序列中逐个选取元素,也就是常用的 for in 结构。可以有多个for in,比如 for x in xxx for y in yyy。使用多个for的时候,就会生成所有组合。

colors = ['black','white']
sizes = ['S','M','L']
tshirts = [(color, size) for color in colors for size in sizes]
tshirts
=========================
[('black', 'S'),
 ('black', 'M'),
 ('black', 'L'),
 ('white', 'S'),
 ('white', 'M'),
 ('white', 'L')]

然后看C,C提供一个判断条件,格式一般为 if xxx,其中可以用到B所给出的x,用于选取符合条件的条目。可以省略,也就意味着使用B生成的所有条目。

最后看A,A格式随意,可以使用B中给出的x(或者y),当然也可以不使用。

生成器表达式和列表推导唯一的不同是用 () 包围而不是 [],如果不需要一次性生成整个列表,那么用生成器表达式更好。(word for word in words if word.islower())
==========================================
<generator object <genexpr> at 0x000001B1C18094C0>

需要变成列表的时候也可以随时调用 list() 函数转化为列表。
list((word for word in words if word.islower()))
=================================================
['quick', 'jumed', 'the']

二、元组(Tuple)
元组的主要用途:
1)作为不可变的列表
2)作为没有字段名称的记录

元组解包:
lax_coordinates = (33.9425, -118.408055)
latitude, longitude = lax_coordinates
latitude
==========
33.9425

longitude
============
-118.408055

作为函数参数就地展开,在前面加*就可以了:
>>> divmod(20, 8)
(2, 4)
>>> t = (20, 8)
>>> divmod(*t)
(2, 4)

具名元组(Named Tuples):
因为元组作为记录比较好用,因此出现了 namedtuple,在 collections 模块中。
使用 namedtuple 创建的实例消耗的内存和普通元组相同,因为字段的名字是存储在类中的。他们使用的内存比普通的类要少,因为它们不用在每个实例的 __dict__ 中存储属性。
>>> from collections import namedtuple
>>> Point = namedtuple('Point', 'x y')
>>> p = Point(3, 4)
>>> p
Point(x=3, y=4)

namedtuple 有几个有用的属性和方法:
1)_fields 类属性,保存所有字段名称
>>> Point._fields
('x', 'y')

2)_make(iterable) 类方法,使用已经存在的序列或者 iterable 来创建 namedtuple
>>> point_tuple = (3, 4)
>>> p = Point._make(point_tuple)
>>> p
Point(x=3, y=4)

3)_asdict() 实例方法,返回一个 OrderedDict,映射名称和对应的值。
>>> p._asdict()
OrderedDict([('x', 3), ('y', 4)])

三、切片(Slice)
切片是序列中非常常用的操作,基本格式是 seq[start:stop:step],其中step及之前的冒号均可以省略,表示 step 为1。start和stop也都可以省略,但是第一个冒号不能省略。start省略表示从头开始,stop省略表示切到尾部。

切片操作适用于几乎所有序列类型,如 list, tuple, str 等等。

Python 中表示范围一般使用前闭后开 [start, stop),根据这两个单词也比较容易记忆。

切片的长度,自然也就是 stop - start。

四、字典(Dict)
1、构造方式示例:
>>> d1 = dict(one=1, two=2, three=3)
>>> d2 = {'one':1, 'two':2, 'three':3}
>>> d3 = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d4 = dict([('two',2), ('one', 1), ('three', 3)])
>>> d5 = dict({'three':3, 'one':1, 'two':2})
>>> d1 == d2 == d3 == d4 == d5
True

2、字典推导示例:
>>> d = {n: n ** 2 for n in range(5)}
>>> d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

3、处理缺失key值的几种方法:
1)setdefault 方法
my_dict.setdefault(key, []).append(new_value)

等同于:
if key not in my_dict:
    my_dict[key] = []

my_dict[key].append(new_value)

2)defaultdict
如果一开始就知道这个字典可能经常找不到key,想提供一个默认值,那么可以使用 defaultdict。
工作原理:当实例化一个 defaultdict 时,提供一个函数 default_factory,每次 __getitem__ 传入不存在的 key 参数时,产生默认值。

3、常用映射模块:
1)collections.OrderedDict
根据插入顺序来管理 key,遍历 item 的顺序可以预期。 popitem 默认返回第一个 item,但是如果调用
my_odict.popitem(last=True),则弹出最后一个添加的条目。

2)collections.Counter
持有每个 key 的数量,每次 update 一个已经有的 key 都会增加其数量。这个可以用来为可哈希的对象计数,或者用来作为多重集合(每个元素可以出现多次)。
>>> import collections
>>> ct = collections.Counter('abracadabra')
>>> ct
Counter({'a': 5, 'r': 2, 'b': 2, 'c': 1, 'd': 1})
>>> ct.update('aaazzz')
>>> ct
Counter({'a': 8, 'z': 3, 'r': 2, 'b': 2, 'd': 1, 'c': 1})
>>> ct.most_common(2)
[('a', 8), ('z', 3)]

3)collections.UserDict
纯 Python 实现的映射,和标准的 dict 工作方式一样,设计用来被继承的。

五、集合(Set)
集合的主要作用还是去重:
>>> l = ['spam', 'spam', 'eggs', 'spam']
>>> set(l)
{'spam', 'eggs'}
>>> list(set(l))
['spam', 'eggs']

原创文章 136 获赞 51 访问量 10万+

猜你喜欢

转载自blog.csdn.net/zhaocen_1230/article/details/82257550