5. 数据结构¶
本章深入讲解之前学过的一些内容,同时,还增加了新的知识点。5.1. 列表详解
列表数据类型支持很多方法,列表对象的所有方法所示如下: list.append(x) 在列表末尾添加一个元素,相当于<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a[len(a):]</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">[x]</font> 。
list.extend(iterable)
用可迭代对象的元素扩展列表。相当于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a[len(a):]</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">iterable</font> 。
list.insert(i, x)
在指定位置插入元素。第一个参数是插入元素的索引,因此,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a.insert(0,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">x)</font> 在列表开头插入元素, <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a.insert(len(a),</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">x)</font> 等同于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a.append(x)</font> 。
list.remove(x)
从列表中删除第一个值为 x 的元素。未找到指定元素时,触发 ValueError 异常。
list.pop([i])
移除列表中给定位置上的条目,并返回该条目。 如果未指定索引号,则 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a.pop()</font> 将移除并返回列表中的最后一个条目。 如果列表为空或索引号在列表索引范围之外则会引发 IndexError。
list.clear()
删除列表里的所有元素,相当于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">del</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a[:]</font> 。
list.index(x[, start[, end]])
返回列表中第一个值为 x 的元素的零基索引。未找到指定元素时,触发 ValueError 异常。
可选参数 start 和 end 是切片符号,用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的,而不是 start 参数。
list.count(x)
返回列表中元素 x 出现的次数。
list.sort(*, key=None, reverse=False)
就地排序列表中的元素(要了解自定义排序参数,详见 sorted())。
list.reverse()
翻转列表中的元素。
list.copy()
返回列表的浅拷贝。相当于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a[:]</font> 。
多数列表方法示例:
>>>
你可能已经注意到
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']>>> fruits.count('apple')2>>> fruits.count('tangerine')0>>> fruits.index('banana')3>>> fruits.index('banana', 4) # Find next banana starting at position 46>>> fruits.reverse()>>> fruits['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']>>> fruits.append('grape')>>> fruits['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']>>> fruits.sort()>>> fruits['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']>>> fruits.pop()'pear'
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">insert</font>, <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">remove</font> 或 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">sort</font> 等仅修改列表的方法都不会打印返回值 — 它们返回默认值 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font>。 [1] 这是适用于 Python 中所有可变数据结构的设计原则。
你可能会注意到的另一件事是并非所有数据都可以排序或比较。 举例来说,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">[None,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'hello',</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">10]</font> 就不可排序因为整数不能与字符串比较而 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">None</font> 不能与其他类型比较。 此外,还存在一些没有定义顺序关系的类型。 例如,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">3+4j</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"><</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">5+7j</font> 就不是一个合法的比较。
5.1.1. 用列表实现堆栈
通过列表方法可以非常容易地将列表作为栈来使用,最后添加的元素将最先被提取(“后进先出”)。 要向栈顶添加一个条目,请使用<font style="color:rgb(0, 0, 0);">append()</font>。 要从栈顶提取一个条目,请使用 <font style="color:rgb(0, 0, 0);">pop()</font>,无需显式指定索引。 例如:
>>>
>>> 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]
5.1.2. 用列表实现队列
列表也可以用作队列,最先加入的元素,最先取出(“先进先出”);然而,列表作为队列的效率很低。因为,在列表末尾添加和删除元素非常快,但在列表开头插入或移除元素却很慢(因为所有其他元素都必须移动一位)。 实现队列最好用 collections.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 arrivaldeque(['Michael', 'Terry', 'Graham'])
5.1.3. 列表推导式
列表推导式创建列表的方式更简洁。常见的用法为,对序列或可迭代对象中的每个元素应用某种操作,用生成的结果创建新的列表;或用满足特定条件的元素创建子序列。 例如,创建平方值的列表: >>>注意,这段代码创建(或覆盖)变量
>>> squares = []>>> for x in range(10):... squares.append(x**2)...>>> squares[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">x</font>,该变量在循环结束后仍然存在。下述方法可以无副作用地计算平方列表:
或等价于:
squares = list(map(lambda x: x**2, range(10)))
上面这种写法更简洁、易读。 列表推导式的方括号内包含以下内容:一个表达式,后面为一个
squares = [x**2 for x in range(10)]
<font style="color:rgb(0, 0, 0);">for</font> 子句,然后,是零个或多个 <font style="color:rgb(0, 0, 0);">for</font> 或 <font style="color:rgb(0, 0, 0);">if</font> 子句。结果是由表达式依据 <font style="color:rgb(0, 0, 0);">for</font> 和 <font style="color:rgb(0, 0, 0);">if</font> 子句求值计算而得出一个新列表。 举例来说,以下列表推导式将两个列表中不相等的元素组合起来:
>>>
等价于: >>>
>>> [(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)]
注意,上面两段代码中,for 和 if 的顺序相同。 表达式是元组(例如上例的
>>> combs = []>>> for x in [1,2,3]:... for y in [3,1,4]:... if x != y:... combs.append((x, y))...>>> combs[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(x,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">y)</font>)时,必须加上括号:
>>>
列表推导式可以使用复杂的表达式和嵌套函数: >>>
>>> vec = [-4, -2, 0, 2, 4]>>> # create a new list with the values doubled>>> [x*2 for x in vec][-8, -4, 0, 4, 8]>>> # filter the list to exclude negative numbers>>> [x for x in vec if x >= 0][0, 2, 4]>>> # apply a function to all the elements>>> [abs(x) for x in vec][4, 2, 0, 2, 4]>>> # call a method on each element>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']>>> [weapon.strip() for weapon in freshfruit]['banana', 'loganberry', 'passion fruit']>>> # create a list of 2-tuples like (number, square)>>> [(x, x**2) for x in range(6)][(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]>>> # the tuple must be parenthesized, otherwise an error is raised>>> [x, x**2 for x in range(6)]File "<stdin>", line 1[x, x**2 for x in range(6)]^^^^^^^SyntaxError: did you forget parentheses around the comprehension target?>>> # flatten a list using a listcomp with two 'for'>>> vec = [[1,2,3], [4,5,6], [7,8,9]]>>> [num for elem in vec for num in elem][1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from math import pi>>> [str(round(pi, i)) for i in range(1, 6)]['3.1', '3.14', '3.142', '3.1416', '3.14159']
5.1.4. 嵌套的列表推导式
列表推导式中的初始表达式可以是任何表达式,甚至可以是另一个列表推导式。 下面这个 3x4 矩阵,由 3 个长度为 4 的列表组成: >>>下面的列表推导式可以转置行列: >>>
>>> matrix = [... [1, 2, 3, 4],... [5, 6, 7, 8],... [9, 10, 11, 12],... ]
如我们在之前小节中看到的,内部的列表推导式是在它之后的 for 的上下文中被求值的,所以这个例子等价于: >>>
>>> [[row[i] for row in matrix] for i in range(4)][[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
反过来说,也等价于: >>>
>>> transposed = []>>> for i in range(4):... transposed.append([row[i] for row in matrix])...>>> transposed[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
实际应用中,最好用内置函数替代复杂的流程语句。此时,zip() 函数更好用: >>>
>>> transposed = []>>> for i in range(4):... # the following 3 lines implement the nested listcomp... transposed_row = []... for row in matrix:... transposed_row.append(row[i])... transposed.append(transposed_row)...>>> transposed[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
关于本行中星号的详细说明,参见 解包实参列表。
>>> list(zip(*matrix))[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
5.2. del 语句
有一种方式可以按索引而不是值从列表中移除条目: del 语句。 这与返回一个值的 <font style="color:rgb(0, 0, 0);">pop()</font> 方法不同。 <font style="color:rgb(0, 0, 0);">del</font> 语句也可用于从列表中移除切片或清空整个列表(我们之前通过将切片赋值为一个空列表实现过此操作)。 例如:
>>>
>>> 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[]
del 也可以用来删除整个变量:
>>>此后,再引用
>>> del a
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a</font> 就会报错(直到为它赋与另一个值)。后文会介绍 del 的其他用法。
5.3. 元组和序列
列表和字符串有很多共性,例如,索引和切片操作。这两种数据类型是 序列 (参见 序列类型 —- list, tuple, range)。随着 Python 语言的发展,其他的序列类型也被加入其中。本节介绍另一种标准序列类型:元组。 元组由多个用逗号隔开的值组成,例如: >>>输出时,元组都要由圆括号标注,这样才能正确地解释嵌套元组。输入时,圆括号可有可无,不过经常是必须的(如果元组是更大的表达式的一部分)。不允许为元组中的单个元素赋值,当然,可以创建含列表等可变对象的元组。 虽然,元组与列表很像,但使用场景不同,用途也不同。元组是 immutable (不可变的),一般可包含异质元素序列,通过解包(见本节下文)或索引访问(如果是 namedtuples,可以属性访问)。列表是 mutable (可变的),列表元素一般为同质类型,可迭代访问。 构造 0 个或 1 个元素的元组比较特殊:为了适应这种情况,对句法有一些额外的改变。用一对空圆括号就可以创建空元组;只有一个元素的元组可以通过在这个元素后添加逗号来构建(圆括号里只有一个值的话不够明确)。丑陋,但是有效。例如: >>>
>>> t = 12345, 54321, 'hello!'>>> t[0]12345>>> t(12345, 54321, 'hello!')>>> # Tuples may be nested:... u = t, (1, 2, 3, 4, 5)>>> u((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))>>> # Tuples are immutable:... t[0] = 88888Traceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment>>> # but they can contain mutable objects:... v = ([1, 2, 3], [3, 2, 1])>>> v([1, 2, 3], [3, 2, 1])
语句
>>> empty = ()>>> singleton = 'hello', # <-- note trailing comma>>> len(empty)0>>> len(singleton)1>>> singleton('hello',)
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">t</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">12345,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">54321,</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'hello!'</font> 是 元组打包 的例子:值 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">12345</font>, <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">54321</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">'hello!'</font> 一起被打包进元组。逆操作也可以:
>>>
称之为 序列解包 也是妥妥的,适用于右侧的任何序列。序列解包时,左侧变量与右侧序列元素的数量应相等。注意,多重赋值其实只是元组打包和序列解包的组合。
>>> x, y, z = t
5.4. 集合
Python 还支持 集合 这种数据类型。集合是由不重复元素组成的无序容器。基本用法包括成员检测、消除重复元素。集合对象支持合集、交集、差集、对称差分等数学运算。 创建集合用花括号或 set() 函数。注意,创建空集合只能用<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">set()</font>,不能用 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">{}</font>,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">{}</font> 创建的是空字典,下一小节介绍数据结构:字典。
以下是一些简单的示例
>>>
与 列表推导式 类似,集合也支持推导式: >>>
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}>>> print(basket) # show that duplicates have been removed{'orange', 'banana', 'pear', 'apple'}>>> 'orange' in basket # fast membership testingTrue>>> 'crabgrass' in basketFalse>>> # Demonstrate set operations on unique letters from two words...>>> 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'}
>>> a = {x for x in 'abracadabra' if x not in 'abc'}>>> a{'r', 'd'}
5.5. 字典
另一个非常有用的 Python 内置数据类型是 字典 (参见 映射类型 —- dict)。 字典在其他语言中可能会被称为“关联存储”或“关联数组”。 不同于以固定范围的数字进行索引的序列,字典是以 键 进行索引的,键可以是任何不可变类型;字符串和数字总是可以作为键。 如果一个元组只包含字符串、数字或元组则也可以作为键;如果一个元组直接或间接地包含了任何可变对象,则不能作为键。 列表不能作为键,因为列表可以使用索引赋值、切片赋值或者<font style="color:rgb(0, 0, 0);">append()</font> 和 <font style="color:rgb(0, 0, 0);">extend()</font> 等方法进行原地修改列表。
可以把字典理解为 键值对 的集合,但字典的键必须是唯一的。花括号 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">{}</font> 用于创建空字典。另一种初始化字典的方式是,在花括号里输入逗号分隔的键值对,这也是字典的输出方式。
字典的主要用途是通过关键字存储、提取值。用 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">del</font> 可以删除键值对。用已存在的关键字存储值,与该关键字关联的旧值会被取代。通过不存在的键提取值,则会报错。
对字典执行 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">list(d)</font> 操作,返回该字典中所有键的列表,按插入次序排列(如需排序,请使用 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">sorted(d)</font>)。检查字典里是否存在某个键,使用关键字 in。
以下是一些字典的简单示例:
>>>
>>> tel = {'jack': 4098, 'sape': 4139}>>> tel['guido'] = 4127>>> tel{'jack': 4098, 'sape': 4139, 'guido': 4127}>>> tel['jack']4098>>> del tel['sape']>>> tel['irv'] = 4127>>> tel{'jack': 4098, 'guido': 4127, 'irv': 4127}>>> list(tel)['jack', 'guido', 'irv']>>> sorted(tel)['guido', 'irv', 'jack']>>> 'guido' in telTrue>>> 'jack' not in telFalse
dict() 构造函数可以直接用键值对序列创建字典:
>>>字典推导式可以用任意键值表达式创建字典: >>>
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]){'sape': 4139, 'guido': 4127, 'jack': 4098}
关键字是比较简单的字符串时,直接用关键字参数指定键值对更便捷: >>>
>>> {x: x**2 for x in (2, 4, 6)}{2: 4, 4: 16, 6: 36}
>>> dict(sape=4139, guido=4127, jack=4098){'sape': 4139, 'guido': 4127, 'jack': 4098}
5.6. 循环的技巧
当对字典执行循环时,可以使用 items() 方法同时提取键及其对应的值。 >>>在序列中循环时,用 enumerate() 函数可以同时取出位置索引和对应的值: >>>
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}>>> for k, v in knights.items():... print(k, v)...gallahad the purerobin the brave
同时循环两个或多个序列时,用 zip() 函数可以将其内的元素一一匹配: >>>
>>> for i, v in enumerate(['tic', 'tac', 'toe']):... print(i, v)...0 tic1 tac2 toe
为了逆向对序列进行循环,可以求出欲循环的正向序列,然后调用 reversed() 函数: >>>
>>> 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.
按指定顺序循环序列,可以用 sorted() 函数,在不改动原序列的基础上,返回一个重新的序列: >>>
>>> for i in reversed(range(1, 10, 2)):... print(i)...97531
使用 set() 去除序列中的重复元素。使用 sorted() 加 set() 则按排序后的顺序,循环遍历序列中的唯一元素: >>>
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']>>> for i in sorted(basket):... print(i)...appleapplebananaorangeorangepear
一般来说,在循环中修改列表的内容时,创建新列表比较简单,且安全: >>>
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']>>> for f in sorted(set(basket)):... print(f)...applebananaorangepear
>>> import math>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]>>> filtered_data = []>>> for value in raw_data:... if not math.isnan(value):... filtered_data.append(value)...>>> filtered_data[56.2, 51.7, 55.3, 52.5, 47.8]
5.7. 深入条件控制
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">while</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">if</font> 条件句不只可以进行比较,还可以使用任意运算符。
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">in</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">not</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">in</font> 用于执行确定一个值是否存在(或不存在)于某个容器中的成员检测。 运算符 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">is</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">is</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">not</font> 用于比较两个对象是否是同一个对象。 所有比较运算符的优先级都一样,且低于任何数值运算符。
比较操作支持链式操作。例如,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"><</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">b</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">==</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">c</font> 校验 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">a</font> 是否小于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">b</font>,且 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">b</font> 是否等于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">c</font>。
比较操作可以用布尔运算符 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font> 组合,并且,比较操作(或其他布尔运算)的结果都可以用 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">not</font> 取反。这些操作符的优先级低于比较操作符;<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">not</font> 的优先级最高, <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font> 的优先级最低,因此,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">A</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">not</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">B</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">C</font> 等价于 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(A</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">(not</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">B))</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">C</font>。与其他运算符操作一样,此处也可以用圆括号表示想要的组合。
布尔运算符 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">or</font> 是所谓的 短路 运算符:其参数从左至右求值,一旦可以确定结果,求值就会停止。例如,如果 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">A</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">C</font> 为真,<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">B</font> 为假,那么 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">A</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">B</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">and</font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"> </font><font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">C</font> 不会对 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">C</font> 求值。用作普通值而不是布尔值时,短路运算符的返回值通常是最后一个求了值的参数。
还可以把比较运算或其它布尔表达式的结果赋值给变量,例如:
>>>
注意,Python 与 C 不同,在表达式内部赋值必须显式使用 海象运算符
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'>>> non_null = string1 or string2 or string3>>> non_null'Trondheim'
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">:=</font>。 这避免了 C 程序中常见的问题:要在表达式中写 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">==</font> 时,却写成了 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">=</font>。
5.8. 序列和其他类型的比较
序列对象可以与相同序列类型的其他对象比较。这种比较使用 字典式 顺序:首先,比较前两个对应元素,如果不相等,则可确定比较结果;如果相等,则比较之后的两个元素,以此类推,直到其中一个序列结束。如果要比较的两个元素本身是相同类型的序列,则递归地执行字典式顺序比较。如果两个序列中所有的对应元素都相等,则两个序列相等。如果一个序列是另一个的初始子序列,则较短的序列可被视为较小(较少)的序列。 对于字符串来说,字典式顺序使用 Unicode 码位序号排序单个字符。下面列出了一些比较相同类型序列的例子:注意,当比较不同类型的对象时,只要待比较的对象提供了合适的比较方法,就可以使用
(1, 2, 3) < (1, 2, 4)[1, 2, 3] < [1, 2, 4]'ABC' < 'C' < 'Pascal' < 'Python'(1, 2, 3, 4) < (1, 2, 4)(1, 2) < (1, 2, -1)(1, 2, 3) == (1.0, 2.0, 3.0)(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);"><</font> 和 <font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">></font> 进行比较。例如,混合的数字类型通过数字值进行比较,所以,0 等于 0.0,等等。如果没有提供合适的比较方法,解释器不会随便给出一个比较结果,而是引发 TypeError 异常。
备注
[1] 别的语言可能会将可变对象返回,允许方法连续执行,例如<font style="color:rgb(0, 0, 0);background-color:rgb(236, 240, 243);">d->insert("a")->remove("b")->sort();</font>。
