From 8aed051baca0518d0103e9ea28b9c85fe3faafd5 Mon Sep 17 00:00:00 2001 From: sptzxbbb Date: Thu, 21 Jul 2016 15:46:36 +0800 Subject: [PATCH] Fix a few format errors and typos in ch01 and ch02 --- source/c01/p05_implement_a_priority_queue.rst | 2 +- source/c01/p06_map_keys_to_multiple_values_in_dict.rst | 2 +- source/c01/p13_sort_list_of_dicts_by_key.rst | 2 +- source/c01/p15_group_records_based_on_field.rst | 2 +- source/c01/p18_map_names_to_sequence_elements.rst | 2 +- source/c02/p11_strip_unwanted_characters.rst | 2 +- source/c02/p12_sanitizing_clean_up_text.rst | 2 +- source/c02/p18_tokenizing_text.rst | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/c01/p05_implement_a_priority_queue.rst b/source/c01/p05_implement_a_priority_queue.rst index 68fe840..480aed8 100644 --- a/source/c01/p05_implement_a_priority_queue.rst +++ b/source/c01/p05_implement_a_priority_queue.rst @@ -103,7 +103,7 @@ >>> 通过引入另外的 ``index`` 变量组成三元组 ``(priority, index, item)`` ,就能很好的避免上面的错误, -因为不可能有两个元素有相同的 ``index`` 值。Python在做元组比较时候,如果前面的比较以及可以确定结果了, +因为不可能有两个元素有相同的 ``index`` 值。Python在做元组比较时候,如果前面的比较已经可以确定结果了, 后面的比较操作就不会发生了: .. code-block:: python diff --git a/source/c01/p06_map_keys_to_multiple_values_in_dict.rst b/source/c01/p06_map_keys_to_multiple_values_in_dict.rst index 55a1f91..4a666d2 100644 --- a/source/c01/p06_map_keys_to_multiple_values_in_dict.rst +++ b/source/c01/p06_map_keys_to_multiple_values_in_dict.rst @@ -54,7 +54,7 @@ d.setdefault('a', []).append(2) d.setdefault('b', []).append(4) -但是很多程序员觉得 ``setdefault()`` 用起来有点别扭。因为每次调用都得创建一个新的初始值的实例(例子程序中的空列表[])。 +但是很多程序员觉得 ``setdefault()`` 用起来有点别扭。因为每次调用都得创建一个新的初始值的实例(例子程序中的空列表 ``[]`` )。 ---------- 讨论 diff --git a/source/c01/p13_sort_list_of_dicts_by_key.rst b/source/c01/p13_sort_list_of_dicts_by_key.rst index a5a158c..ac6e117 100644 --- a/source/c01/p13_sort_list_of_dicts_by_key.rst +++ b/source/c01/p13_sort_list_of_dicts_by_key.rst @@ -68,7 +68,7 @@ 这个参数是 ``callable`` 类型,并且从 ``rows`` 中接受一个单一元素,然后返回被用来排序的值。 ``itemgetter()`` 函数就是负责创建这个 ``callable`` 对象的。 -``operator.itemgetter()`` 函数有一个被rows中的记录用来查找值的索引参数。可以是一个字典键名称, +``operator.itemgetter()`` 函数有一个被 ``rows`` 中的记录用来查找值的索引参数。可以是一个字典键名称, 一个整形值或者任何能够传入一个对象的 ``__getitem__()`` 方法的值。 如果你传入多个索引参数给 ``itemgetter()`` ,它生成的 ``callable`` 对象会返回一个包含所有元素值的元组, 并且 ``sorted()`` 函数会根据这个元组中元素顺序去排序。 diff --git a/source/c01/p15_group_records_based_on_field.rst b/source/c01/p15_group_records_based_on_field.rst index 8fd3786..f8684d8 100644 --- a/source/c01/p15_group_records_based_on_field.rst +++ b/source/c01/p15_group_records_based_on_field.rst @@ -69,7 +69,7 @@ 一个非常重要的准备步骤是要根据指定的字段将数据排序。 因为 ``groupby()`` 仅仅检查连续的元素,如果事先并没有排序完成的话,分组函数将得不到想要的结果。 -如果你仅仅只是想根据date字段将数据分组到一个大的数据结构中去,并且允许随机访问, +如果你仅仅只是想根据 ``date`` 字段将数据分组到一个大的数据结构中去,并且允许随机访问, 那么你最好使用 ``defaultdict()`` 来构建一个多值字典,关于多值字典已经在1.6小节有过详细的介绍。比如: .. code-block:: python diff --git a/source/c01/p18_map_names_to_sequence_elements.rst b/source/c01/p18_map_names_to_sequence_elements.rst index 05fefe0..bd2681a 100644 --- a/source/c01/p18_map_names_to_sequence_elements.rst +++ b/source/c01/p18_map_names_to_sequence_elements.rst @@ -90,7 +90,7 @@ AttributeError: can't set attribute >>> -如果你真的需要改变然后的属性,那么可以使用命名元组实例的 ``_replace()`` 方法, +如果你真的需要改变属性的值,那么可以使用命名元组实例的 ``_replace()`` 方法, 它会创建一个全新的命名元组并将对应的字段用新的值取代。比如: .. code-block:: python diff --git a/source/c02/p11_strip_unwanted_characters.rst b/source/c02/p11_strip_unwanted_characters.rst index 89be1d9..b2c66a6 100644 --- a/source/c02/p11_strip_unwanted_characters.rst +++ b/source/c02/p11_strip_unwanted_characters.rst @@ -71,6 +71,6 @@ 在这里,表达式 ``lines = (line.strip() for line in f)`` 执行数据转换操作。 这种方式非常高效,因为它不需要预先读取所有数据放到一个临时的列表中去。 -它仅仅只是创建一个生成器,并且每次返回行之前会先执行strip操作。 +它仅仅只是创建一个生成器,并且每次返回行之前会先执行 ``strip`` 操作。 对于更高阶的strip,你可能需要使用 ``translate()`` 方法。请参阅下一节了解更多关于字符串清理的内容。 diff --git a/source/c02/p12_sanitizing_clean_up_text.rst b/source/c02/p12_sanitizing_clean_up_text.rst index 937f864..b94e69a 100644 --- a/source/c02/p12_sanitizing_clean_up_text.rst +++ b/source/c02/p12_sanitizing_clean_up_text.rst @@ -58,7 +58,7 @@ 'python is awesome\n' >>> -上面例子中,通过使用 ``dict.fromkeys()`` 方法构造一个字典,每个Unicode和音符作为键,对于的值全部为 ``None`` 。 +上面例子中,通过使用 ``dict.fromkeys()`` 方法构造一个字典,每个Unicode和音符作为键,对应的值全部为 ``None`` 。 然后使用 ``unicodedata.normalize()`` 将原始输入标准化为分解形式字符。 然后再调用 ``translate`` 函数删除所有重音符。 diff --git a/source/c02/p18_tokenizing_text.rst b/source/c02/p18_tokenizing_text.rst index 1cc3c6f..29b0f46 100644 --- a/source/c02/p18_tokenizing_text.rst +++ b/source/c02/p18_tokenizing_text.rst @@ -22,7 +22,7 @@ .. code-block:: python tokens = [('NAME', 'foo'), ('EQ','='), ('NUM', '23'), ('PLUS','+'), - ('NUM', '42'), ('TIMES', '*'), ('NUM', 10')] + ('NUM', '42'), ('TIMES', '*'), ('NUM', '10')] 为了执行这样的切分,第一步就是像下面这样利用命名捕获组的正则表达式来定义所有可能的令牌,包括空格: