Data Structures,Python Tutorial阅读笔记(2)

  参考资料:

  Python官网Tutorial

  注:由于感觉自己的Python还没有学通透,在看项目的代码时还是有一些困难。所以想看一下Python官网的Tutorial自学一下,我在读的时候也是略过了自己已经会的地方,所以我写的东西都是自己学到的新东西。

  规范:黑体x)表示自己学到的东西模块,是一个大概的区分。4.1,4.2等表示在Tutorial中的位置。

  1)5.1.1. Using Lists as Stacks

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

  以前一直不知道一个简单的列表就能当栈数据结构来使用,上面是tutorial给出的例子,进栈和出栈分别是list.append()和list.pop()方法。

  2)尽管列表也可以用作队列,但是效率不高。tutorial建议我们使用deque,下面是一个例子。

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

  可以看到,deque这个方法可以将list再封装一下,就变成了双向队列。利用deque的popleft和append方法可以方便地进行进队列和出队列。

  3)5.1.3 List Comprehensions的概念。这个名词很奇怪,它的英文解释是这样的:List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

  翻译过来是这样:List Comprehensions这个东西用来生成一个列表,这个列表的每个元素-都是对一个序列或者可迭代对象中的每一个元素-加上一个操作产生的。或者是仅取这些元素中满足某些条件的部分。

  用法如下:

squares = [x**2 for x in range(10)]

  在教程中有一个小知识点,我也一并介绍了-关于map()这个函数:它的作用是:第一个参数是一个函数(可以是匿名的),第二个参数是一个可迭代对象。map函数返回一个可迭代对象,当然,是作用于函数之后的。

map(func, *iterables) --> map object

Make an iterator that computes the function using arguments from
each of the iterables.  Stops when the shortest iterable is exhausted.

  回归正题,List Comprehensions还有这样的用法:

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

  总之,List Comprehensions是为了方便和可读性,后面的嵌套List Comprehensions不再介绍。

  4)介绍一个出现的知识点,关于zip()和*,是一个非常有趣的用法,相信读者看完这个例子对zip和*的理解会深不少。先放一下zip的文档:

zip(*iterables) --> zip object

Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument.  The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.

  下面是一个有趣的例子:

# 假设我们有如下的矩阵
>>> matrix = [
...     [1, 2, 3, 4],
...     [5, 6, 7, 8],
...     [9, 10, 11, 12],
... ]
>>>>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

  即矩阵被转置了,分析一下。*matrix将matrix分成了三个可迭代对象即[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]。然后zip方法会取(1,5,9), (2, 6, 10)这样组成一个新的可迭代对象。化成列表就好像被转置了一样。

  5)5.2 使用del方法可以删除列表中的某些项,或者整个变量

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

  6)5.3 元组的概念,Python会将逗号隔开的各个对象看成是元组tuple。tuple是不可变的,但tuple中的元素可以是可变的。tuple有两个标志性的动作即打包(packing)和解包(unpacking)

# 打包
t = 12345, 54321, 'hello!'
# 解包
x, y, z = t

  7)集合的概念,主要的新东西就是集合的几种运算,如下所示:

>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

  8)字典的概念没什么新东西,下面介绍一个非常非常常见的用法,也是和zip有关:

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}?  It is {1}.'.format(q, a))
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

  可见zip将两个可迭代对象并成了一个可迭代对象,这样就可以在一个循环中按需取出对应的元组了。

posted @ 2020-11-19 22:36  思念殇千寻  阅读(143)  评论(0编辑  收藏  举报