diff --git a/.gitignore b/.gitignore index d163863..67152c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -build/ \ No newline at end of file +build/ +.idea/ +*.pyc \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..fb08791 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +python3-cookbook \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..c66df00 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d6fcf67 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bfaa75b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/python3-cookbook.iml b/.idea/python3-cookbook.iml new file mode 100644 index 0000000..a34a857 --- /dev/null +++ b/.idea/python3-cookbook.iml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c80f219 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..c6274b0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,518 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1408877753597 + 1408877753597 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cookbook/__init__.py b/cookbook/__init__.py new file mode 100644 index 0000000..0baca96 --- /dev/null +++ b/cookbook/__init__.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: sample +Desc : +""" + diff --git a/cookbook/c01/__init__.py b/cookbook/c01/__init__.py new file mode 100644 index 0000000..0baca96 --- /dev/null +++ b/cookbook/c01/__init__.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: sample +Desc : +""" + diff --git a/source/aboutme.rst b/source/aboutme.rst index 18ae9a6..ce235db 100644 --- a/source/aboutme.rst +++ b/source/aboutme.rst @@ -9,11 +9,15 @@ * 博客: http://yidao620c.iteye.com/ * GitHub: https://github.com/yidao620c +| +| +| + *主要贡献者* 1. 夏丹 (767962863@qq.com) -2. 梁永威 (415320557@qq.com) -3. 莫浩文 (453166604@qq.com) +2. 张三 (zhangsan@gmail.com) +3. lisi (lisi@gmail.com) | | diff --git a/source/c01/p01_unpack_sequence_into_separate_variables.rst b/source/c01/p01_unpack_sequence_into_separate_variables.rst index f6e9479..0f2dae2 100644 --- a/source/c01/p01_unpack_sequence_into_separate_variables.rst +++ b/source/c01/p01_unpack_sequence_into_separate_variables.rst @@ -5,14 +5,79 @@ ---------- 问题 ---------- -You have an N-element tuple or sequence that you would like to unpack into a collection of N variables. +现在有一个包含N个元素的元祖或者是序列,请问怎样将它里面的值解压后同时赋值给N变量 ---------- 解决方案 ---------- -Any sequence (or iterable) can be unpacked into variables using a simple assignment operation. The only requirement is that the number of variables and structure match the sequence. For example: +任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多个变量。 +唯一的前提就是变量的数量必须跟序列元素的数量是一样的。代码示例: + +.. code-block:: python + + >>> p = (4, 5) + >>> x, y = p + >>> x + 4 + >>> y + 5 + >>> + >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] + >>> name, shares, price, date = data + >>> name + 'ACME' + >>> date + (2012, 12, 21) + >>> name, shares, price, (year, mon, day) = data + >>> name + 'ACME' + >>> year + 2012 + >>> mon + 12 + >>> day + 21 + >>> + +如果变量个数和序列元素的个数不匹配,会产生一个异常,代码示例: + +.. code-block:: python + + >>> p = (4, 5) + >>> x, y, z = p + Traceback (most recent call last): + File "", line 1, in + ValueError: need more than 2 values to unpack + >>> ---------- 讨论 ---------- -Unpacking actually works with any object that happens to be iterable, not just tuples or lists. This includes strings, files, iterators, and generators. For example: \ No newline at end of file +实际上,这种解压赋值可以用在任何可迭代对象上面,而不仅仅是列表或者元祖。 +包括字符串,文件对象,迭代器和生成器。代码示例: + +.. code-block:: python + + >>> s = 'Hello' + >>> a, b, c, d, e = s + >>> a + 'H' + >>> b + 'e' + >>> e + 'o' + >>> + +有时候,你可能只想解压一部分,丢弃其他的值。对于这种情况Python并没有提供特殊的语法。 +但是你可以使用任意变量名去占位,到时候丢掉这些变量就行了。代码示例: +.. code-block::python + + >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] + >>> _, shares, price, _ = data + >>> shares + 50 + >>> price + 91.1 + >>> + +你必须保证你选用的那些占位变量名在其他地方没被使用到。 \ No newline at end of file diff --git a/source/chapters/p01_data_structures_algorithms.rst b/source/chapters/p01_data_structures_algorithms.rst index c39aa8c..4b8168e 100644 --- a/source/chapters/p01_data_structures_algorithms.rst +++ b/source/chapters/p01_data_structures_algorithms.rst @@ -2,12 +2,11 @@ 第一章:数据结构和算法 ============================= -Python provides a variety of useful built-in data structures, such as lists, sets, and dictionaries. -For the most part, the use of these structures is straightforward. However, -common questions concerning searching, sorting, ordering, and filtering often arise. -Thus, the goal of this chapter is to discuss common data structures and algorithms -involving data. In addition, treatment is given to the various data structures contained -in the collections module. +Python提供了大量的内置数据结构,包括列表,集合以及字典。大多数情况下使用这些数据结构式很简单的。 +但是,我们也会经常碰到到诸如查询,排序和过滤等等这些普遍存在的问题。 +因此,这一章的目的就是讨论这些比较常见的问题和算法。 +另外,我们也会给出在集合模块collections当中操作这些数据结构的方法。 + Contents: