github.io 有点慢,font-awesome 有点大,
客官麻烦搬条板凳坐一下下,我马上就到!

Python进阶函数和功能_#3(Collections)

collections 是 Python 内建的一个集合模块,提供了许多有用的集合类。

参考资料:collections - 廖雪峰的官方网站

下面是快捷方式(点击直达)
tuple 和 NamedTupleDefaultDictCounter

tuple 和 NamedTuple

tuple 不可变的相对性

1
2
3
t = ('text', [11, 22])
t[1].append(33)
print(t)

输出结果:

('text', [11, 22, 33])

immutable 的优势

  1. 性能优化:在编译时作为常量确定(python是解释型语言)
  2. 线程安全
  3. 可哈希(list 对象不可以)

可哈希(hashable)和不可改变性(immutable)

  • 如果一个对象在自己的生命周期中有一哈希值(hash value)是不可改变的,那么它就是可哈希的(hashable)的,因为这些数据结构内置了哈希值,每个可哈希的对象都内置了 __hash__ 方法.
  • 所以可哈希的对象可以 通过哈希值进行对比 ,也可以 作为字典的键值作为 set 函数的参数 。Python 中所有不可改变的的对象(imutable objects)都是可哈希的,比如 字符串元组,也就是说可改变的容器如 字典列表 不可哈希(unhashable)。我们用户所定义的 类的实例对象 默认是可哈希的(hashable),它们都是唯一的,而 hash 值也就是它们的id。
  • 因为哈希键一定是不可改变的,所以它们对应的哈希值也不改变。如果允许它们改变,那么它们在数据结构如哈希表中的存储位置也会改变,因此会与哈希的概念违背,效率 会大打折扣。

tuple 拆包

1
2
3
info = ('John Theo', 21, 176, 'Zhejiang University')
name, *others = info
print(name)

输出结果:

John Theo

结果包含了 位置对应 信息

NamedTuple

NamedTuple 相对 class 节省空间,因为少了很多魔术方法

1
2
3
4
5
6
7
8
from collections import namedtuple

User = namedtuple('User', ['name', 'age', 'height'])
user_tuple = ('John_Theo', 21)
user = User(*user_tuple, 176)

print(user.name, user.age, user.height)
print(user._asdict())

输出结果:

John_Theo 21 176
OrderedDict([('name', 'John_Theo'), ('age', 21), ('height', 176)])

NamedTuple 继承 tuple,也支持拆包。

回到目录


DefaultDict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from collections import defaultdict

def default_generator():
return 0

info_dict = defaultdict(default_generator)

user_list = ['John', 'Jack', 'John', 'Jack', 'Jay']
for user in user_list:
info_dict[user] += 1

print(info_dict)

target_users = ['John', 'Jack', 'Jay', 'Jessy']
for user in target_users:
print(info_dict[user], end=' ')

输出结果:

defaultdict(<function default_generator at 0x0000020AB6FA2EA0>, {'John': 2, 'Jack': 2, 'Jay': 1})
2 2 1 0 

回到目录


Counter

1
2
3
4
5
6
7
8
9
from collections import Counter

texts = ['a', 's', 'd', 'a', 's', 'd', 'a', 's', 'd', 'c']
letter_counter = Counter(texts)
print(letter_counter)

letter_counter.update('dwqsadadwa')
print(letter_counter)
print(letter_counter.most_common(2))

输出结果:

Counter({'a': 3, 's': 3, 'd': 3, 'c': 1})
Counter({'a': 6, 'd': 6, 's': 4, 'w': 2, 'c': 1, 'q': 1})
[('a', 6), ('d', 6)]

回到目录




————  EOF  ————