search
HomeBackend DevelopmentPython Tutorial[译]The Python Tutorial#Data Structures

[Translation]The Python Tutorial#Data Structures

5.1 Data Structures

This chapter details some of the previously introduced content, and will also introduce some new content.

5.1 More on Lists

The list data type has more methods, the following are all methods of the list object:

  • list.append(x)
    Adds a new item to the end of the list, equivalent to a[len(a):] = [x]

  • list.extend(iterable)
    Extend the list by adding all items in the iterable object, equivalent to a[len(a):] = iterable

  • list.insert(i, x)
    Inserts an item at the specified position. The first parameter is the element index. The new item will be inserted before this index, so a.insert(0, x) will be inserted at the front of the list, a.insert(len(a ), x) is equivalent to a.append(x).

  • list.remove(x)
    Removes the first item with value x from the list. If x does not exist, the method throws an exception (ValueErrorException)

  • list.pop([i])
    Removes the item at the specified position from the list and returns. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets surrounding i in the method signature indicate that the parameter is optional, instead of writing a square bracket at this position. This notation is often used in the Python Library Reference)

  • list.clear()
    Removes all items from the list, equivalent to del a[:]

  • list.index(x[, start[, end]])
    Returns the 0-based index of the first item with value x, or throws a ValueError exception if x does not exist.
    The optional arguments start and end are interpreted as slice notation and are used to limit the search to a specific sublist of the list. The index returned is relative to the full list index, not relative to the start parameter.

  • list.count(x)
    Returns the number of times x appears in the list

  • list.sort(key=None, reverse=False)
    Sort all items in the list (parameters are used to customize the sorting, see sorted() for more information)

  • list.reverse()
    Reverse list elements

  • list.copy()
    Returns a shallow copy of the list, equivalent to a[:]

The following is an example demonstrating the list method:

<code class="sourceCode python"><span class="op">>>></span> fruits <span class="op">=</span> [<span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'banana'</span>, <span class="st">'kiwi'</span>, <span class="st">'apple'</span>, <span class="st">'banana'</span>]
<span class="op">>>></span> fruits.count(<span class="st">'apple'</span>)
<span class="dv">2</span>
<span class="op">>>></span> fruits.count(<span class="st">'tangerine'</span>)
<span class="dv">0</span>
<span class="op">>>></span> fruits.index(<span class="st">'banana'</span>)
<span class="dv">3</span>
<span class="op">>>></span> fruits.index(<span class="st">'banana'</span>, <span class="dv">4</span>)  <span class="co"># Find next banana starting a position 4</span>
<span class="dv">6</span>
<span class="op">>>></span> fruits.reverse()
<span class="op">>>></span> fruits
[<span class="st">'banana'</span>, <span class="st">'apple'</span>, <span class="st">'kiwi'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>, <span class="st">'orange'</span>]
<span class="op">>>></span> fruits.append(<span class="st">'grape'</span>)
<span class="op">>>></span> fruits
[<span class="st">'banana'</span>, <span class="st">'apple'</span>, <span class="st">'kiwi'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'grape'</span>]
<span class="op">>>></span> fruits.sort()
<span class="op">>>></span> fruits
[<span class="st">'apple'</span>, <span class="st">'apple'</span>, <span class="st">'banana'</span>, <span class="st">'banana'</span>, <span class="st">'grape'</span>, <span class="st">'kiwi'</span>, <span class="st">'orange'</span>, <span class="st">'pear'</span>]
<span class="op">>>></span> fruits.pop()
<span class="co">'pear'</span></code>

Such as insert, reverse or sort only change the list but no return value is printed. They return the default None[1 ]. This is a design principle applicable to Python's mutable data structures.

5.1.1 Using Lists as Stacks

The list method makes it very easy to use it as a stack, and the last element added to the stack is the first to be released (last in, first out). Use the append() method to add elements to the top of the stack, and use pop() without parameters to pop the top element from the stack. Example:

<code class="sourceCode python"><span class="op">>>></span> stack <span class="op">=</span> [<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>]
<span class="op">>>></span> stack.append(<span class="dv">6</span>)
<span class="op">>>></span> stack.append(<span class="dv">7</span>)
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>]
<span class="op">>>></span> stack.pop()
<span class="dv">7</span>
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>]
<span class="op">>>></span> stack.pop()
<span class="dv">6</span>
<span class="op">>>></span> stack.pop()
<span class="dv">5</span>
<span class="op">>>></span> stack
[<span class="dv">3</span>, <span class="dv">4</span>]</code>

5.1.2 Using Lists as Queues

It is also possible to use a list as a queue. The elements added first in the queue are released first (first in, first out); however, using a list in this way is very inefficient. Because adding and removing elements from the end of the list is fast, but inserting or removing elements from the beginning of the list is slow (because all other elements have to be shifted by one).

The specially designed collections.deque adds and deletes elements quickly at both ends, and can be used to implement queues. Example:

<code class="sourceCode python"><span class="op">>>></span> <span class="im">from</span> collections <span class="im">import</span> deque
<span class="op">>>></span> queue <span class="op">=</span> deque([<span class="st">"Eric"</span>, <span class="st">"John"</span>, <span class="st">"Michael"</span>])
<span class="op">>>></span> queue.append(<span class="st">"Terry"</span>)           <span class="co"># Terry arrives</span>
<span class="op">>>></span> queue.append(<span class="st">"Graham"</span>)          <span class="co"># Graham arrives</span>
<span class="op">>>></span> queue.popleft()                 <span class="co"># The first to arrive now leaves</span>
<span class="co">'Eric'</span>
<span class="op">>>></span> queue.popleft()                 <span class="co"># The second to arrive now leaves</span>
<span class="co">'John'</span>
<span class="op">>>></span> queue                           <span class="co"># Remaining queue in order of arrival</span>
deque([<span class="st">'Michael'</span>, <span class="st">'Terry'</span>, <span class="st">'Graham'</span>])</code>

5.1.3 List Comprehensions

List comprehensions provide a concise way to create lists. The general application methods are: creating a new list, the list elements are the results of operations on other sequences or iterable objects; or creating a subsequence whose elements meet specific conditions.

If you want to create a square list:

<code class="sourceCode python"><span class="op">>>></span> squares <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>):
...     squares.append(x<span class="op">**</span><span class="dv">2</span>)
...
<span class="op">>>></span> squares
[<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">9</span>, <span class="dv">16</span>, <span class="dv">25</span>, <span class="dv">36</span>, <span class="dv">49</span>, <span class="dv">64</span>, <span class="dv">81</span>]</code>

Note that the above creates (or rewrites) a variable named x, which still exists after the loop ends. Use the following method to create a squared list without any side effects:

<code class="sourceCode python">squares <span class="op">=</span> <span class="bu">list</span>(<span class="bu">map</span>(<span class="kw">lambda</span> x: x<span class="op">**</span><span class="dv">2</span>, <span class="bu">range</span>(<span class="dv">10</span>)))</code>

Or, etc. used for:

<code class="sourceCode python">squares <span class="op">=</span> [x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">10</span>)]</code>

This method is more concise and readable.

Follows a for clause, followed by zero or more expressions in for clauses or if clauses followed by square brackets, Constitutes a list comprehension. The return result is a new list whose elements are the calculation results of the for and if clauses in the expression. For example, the following list comprehension combines unequal elements in two lists:

<code class="sourceCode python"><span class="op">>>></span> [(x, y) <span class="cf">for</span> x <span class="op">in</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>] <span class="cf">for</span> y <span class="op">in</span> [<span class="dv">3</span>,<span class="dv">1</span>,<span class="dv">4</span>] <span class="cf">if</span> x <span class="op">!=</span> y]
[(<span class="dv">1</span>, <span class="dv">3</span>), (<span class="dv">1</span>, <span class="dv">4</span>), (<span class="dv">2</span>, <span class="dv">3</span>), (<span class="dv">2</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">1</span>), (<span class="dv">3</span>, <span class="dv">4</span>)]</code>

Equivalent to:

<code class="sourceCode python"><span class="op">>>></span> combs <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> x <span class="op">in</span> [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>]:
...     <span class="cf">for</span> y <span class="op">in</span> [<span class="dv">3</span>,<span class="dv">1</span>,<span class="dv">4</span>]:
...         <span class="cf">if</span> x <span class="op">!=</span> y:
...             combs.append((x, y))
...
<span class="op">>>></span> combs
[(<span class="dv">1</span>, <span class="dv">3</span>), (<span class="dv">1</span>, <span class="dv">4</span>), (<span class="dv">2</span>, <span class="dv">3</span>), (<span class="dv">2</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">1</span>), (<span class="dv">3</span>, <span class="dv">4</span>)]</code>

注意上面两个代码段中forif语句的顺序是相同的。

如果表达式是一个元组(如上所示的(x, y)),必须将其加上括号。

<code class="sourceCode python"><span class="op">>>></span> vec <span class="op">=</span> [<span class="op">-</span><span class="dv">4</span>, <span class="op">-</span><span class="dv">2</span>, <span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># create a new list with the values doubled</span>
<span class="op">>>></span> [x<span class="op">*</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> vec]
[<span class="op">-</span><span class="dv">8</span>, <span class="op">-</span><span class="dv">4</span>, <span class="dv">0</span>, <span class="dv">4</span>, <span class="dv">8</span>]
<span class="op">>>></span> <span class="co"># filter the list to exclude negative numbers</span>
<span class="op">>>></span> [x <span class="cf">for</span> x <span class="op">in</span> vec <span class="cf">if</span> x <span class="op">>=</span> <span class="dv">0</span>]
[<span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># apply a function to all the elements</span>
<span class="op">>>></span> [<span class="bu">abs</span>(x) <span class="cf">for</span> x <span class="op">in</span> vec]
[<span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="op">>>></span> <span class="co"># call a method on each element</span>
<span class="op">>>></span> freshfruit <span class="op">=</span> [<span class="st">'  banana'</span>, <span class="st">'  loganberry '</span>, <span class="st">'passion fruit  '</span>]
<span class="op">>>></span> [weapon.strip() <span class="cf">for</span> weapon <span class="op">in</span> freshfruit]
[<span class="st">'banana'</span>, <span class="st">'loganberry'</span>, <span class="st">'passion fruit'</span>]
<span class="op">>>></span> <span class="co"># create a list of 2-tuples like (number, square)</span>
<span class="op">>>></span> [(x, x<span class="op">**</span><span class="dv">2</span>) <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
[(<span class="dv">0</span>, <span class="dv">0</span>), (<span class="dv">1</span>, <span class="dv">1</span>), (<span class="dv">2</span>, <span class="dv">4</span>), (<span class="dv">3</span>, <span class="dv">9</span>), (<span class="dv">4</span>, <span class="dv">16</span>), (<span class="dv">5</span>, <span class="dv">25</span>)]
<span class="op">>>></span> <span class="co"># the tuple must be parenthesized, otherwise an error is raised</span>
<span class="op">>>></span> [x, x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
  File <span class="st">"<stdin>"</span>, line <span class="dv">1</span>, <span class="op">in</span> <span class="op"><</span>module<span class="op">></span>
    [x, x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> <span class="bu">range</span>(<span class="dv">6</span>)]
               <span class="op">^</span>
<span class="pp">SyntaxError</span>: invalid syntax
<span class="op">>>></span> <span class="co"># flatten a list using a listcomp with two 'for'</span>
<span class="op">>>></span> vec <span class="op">=</span> [[<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>], [<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>], [<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>]]
<span class="op">>>></span> [num <span class="cf">for</span> elem <span class="op">in</span> vec <span class="cf">for</span> num <span class="op">in</span> elem]
[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>]</code>

列表推导式可以包含复杂的表达式甚至嵌套函数:

<code class="sourceCode python"><span class="op">>>></span> <span class="im">from</span> math <span class="im">import</span> pi
<span class="op">>>></span> [<span class="bu">str</span>(<span class="bu">round</span>(pi, i)) <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">6</span>)]
[<span class="st">'3.1'</span>, <span class="st">'3.14'</span>, <span class="st">'3.142'</span>, <span class="st">'3.1416'</span>, <span class="st">'3.14159'</span>]</code>

5.1.4 Nested List Comprehensions

列表推导式开头的表达式可以是任意表达式,包括另一个列表推导式。

考虑以下示例,一个包含3个长度为4的列表的列表实现了3x4的矩阵:

<code class="sourceCode python">matrix <span class="op">=</span> [
    [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>],
    [<span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>],
    [<span class="dv">9</span>, <span class="dv">10</span>, <span class="dv">11</span>, <span class="dv">12</span>],
]</code>

以下列表推导式反转行列:

<code class="sourceCode python"><span class="op">>>></span> [[row[i] <span class="cf">for</span> row <span class="op">in</span> matrix] <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>)]
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code>

前面的章节提到,嵌套的列表推导式是在其后跟随的for的上下文中求值的,因此这个示例等同于:

<code class="sourceCode python"><span class="op">>>></span> transposed <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>):
...     transposed.append([row[i] <span class="cf">for</span> row <span class="op">in</span> matrix])
...
<span class="op">>>></span> transposed
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code>

依次等同于:

<code class="sourceCode python"><span class="op">>>></span> transposed <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">range</span>(<span class="dv">4</span>):
...     <span class="co"># the following 3 lines implement the nested listcomp</span>
...     transposed_row <span class="op">=</span> []
...     <span class="cf">for</span> row <span class="op">in</span> matrix:
...         transposed_row.append(row[i])
...     transposed.append(transposed_row)
<span class="op">>>></span> transposed
[[<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>], [<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>], [<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>], [<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>]]</code>

在实践中,应该选择built-in函数来复合流程语句。在以上的用例中zip()函数更有用:

<code class="sourceCode python"><span class="op">>>></span> <span class="bu">list</span>(<span class="bu">zip</span>(<span class="op">*</span>matrix))
[(<span class="dv">1</span>, <span class="dv">5</span>, <span class="dv">9</span>), (<span class="dv">2</span>, <span class="dv">6</span>, <span class="dv">10</span>), (<span class="dv">3</span>, <span class="dv">7</span>, <span class="dv">11</span>), (<span class="dv">4</span>, <span class="dv">8</span>, <span class="dv">12</span>)]</code>

参见 Unpacking Argument Lists了解关于上面*使用的更多详细信息。

5.2 The del statement

在提供列表索引而不是值的情况下,有一种方法可以移除列表中的元素:del语句。这种方式与返回值的pop()方法不同。del语句也可以用来移除部分列表或者清除整个列表(之前使用将空的列表赋值给列表片段的方式实现)。示例:

<code class="sourceCode python"><span class="op">>>></span> a <span class="op">=</span> [<span class="op">-</span><span class="dv">1</span>, <span class="dv">1</span>, <span class="fl">66.25</span>, <span class="dv">333</span>, <span class="dv">333</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[<span class="dv">0</span>]
<span class="op">>>></span> a
[<span class="dv">1</span>, <span class="fl">66.25</span>, <span class="dv">333</span>, <span class="dv">333</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[<span class="dv">2</span>:<span class="dv">4</span>]
<span class="op">>>></span> a
[<span class="dv">1</span>, <span class="fl">66.25</span>, <span class="fl">1234.5</span>]
<span class="op">>>></span> <span class="kw">del</span> a[:]
<span class="op">>>></span> a
[]</code>

del也可以用来删除整个变量:

<code class="sourceCode python"><span class="op">>>></span> <span class="kw">del</span> a</code>

此后引用名字a会抛出异常(至少在其他值赋值给名字a之前)。接下来会有更多del的使用

5.3 Tuples and Sequences

列表和字符串有很多常用属性,比如索引和切片操作。它们是序列数据类型(参见 Sequence Types - list, tuple, range)的两种。Python是一种不断进化的语言,其他的序列类型也可以加入。元组是另一种标准的序列数据类型。

元组包含若干由逗号分隔的值,示例:

<code class="sourceCode python"><span class="op">>>></span> t <span class="op">=</span> <span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>
<span class="op">>>></span> t[<span class="dv">0</span>]
<span class="dv">12345</span>
<span class="op">>>></span> t
(<span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>)
<span class="op">>>></span> <span class="co"># Tuples may be nested:</span>
... u <span class="op">=</span> t, (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>)
<span class="op">>>></span> u
((<span class="dv">12345</span>, <span class="dv">54321</span>, <span class="st">'hello!'</span>), (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>))
<span class="op">>>></span> <span class="co"># Tuples are immutable:</span>
... t[<span class="dv">0</span>] <span class="op">=</span> <span class="dv">88888</span>
Traceback (most recent call last):
  File <span class="st">"<stdin>"</span>, line <span class="dv">1</span>, <span class="op">in</span> <span class="op"><</span>module<span class="op">></span>
<span class="pp">TypeError</span>: <span class="st">'tuple'</span> <span class="bu">object</span> does <span class="op">not</span> support item assignment
<span class="op">>>></span> <span class="co"># but they can contain mutable objects:</span>
... v <span class="op">=</span> ([<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], [<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>])
<span class="op">>>></span> v
([<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>], [<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">1</span>])</code>

可见,输出的元组总是放在圆括号中,以便于嵌套的元组可以被正确解析;虽然圆括号总是必须的(如果元组是其他更大表达式的一部分),但是在输入元组的时候可以选择使用圆括号。不能对元组的单个项赋值,但是可以创建包含如列表的可变对象的元组。

虽然元组和列表有些相似,但是他们通常以不同的目的,用于不同的场景。元组是不可变的,通常包含不同类型的元素,可以通过拆包操作(参见后续章节)或者索引(或者当元组是命名元组时,甚至可以通过属性来访问)来访问。列表是可变的,通常其元素也是不同类型的,可以通过对列表的迭代访问元素。

构建包含零个或者1个项的元组比较特殊:一种额外的奇怪语法可以适用于这种情况。空元组由一对空的圆括号创建;一个元素的元组由一个跟着逗号的值创建(在圆括号中放入单个值是不够的。译注:这种情况:(1)表示整数而不是元组,使用(1, )表示元组也是可行的)。丑陋但是有效。示例:

<code class="sourceCode python"><span class="op">>>></span> empty <span class="op">=</span> ()
<span class="op">>>></span> singleton <span class="op">=</span> <span class="st">'hello'</span>,    <span class="co"># <-- note trailing comma</span>
<span class="op">>>></span> <span class="bu">len</span>(empty)
<span class="dv">0</span>
<span class="op">>>></span> <span class="bu">len</span>(singleton)
<span class="dv">1</span>
<span class="op">>>></span> singleton
(<span class="st">'hello'</span>,)</code>

语句t = 12345, 54321, 'hello!'是封装元组的一个示例:值12345, 54321hello!被封装到了一个元组中。逆向操作也是可行的:

<code class="sourceCode python"><span class="op">>>></span> x, y, z <span class="op">=</span> t</code>

非常恰当地称之为序列解包,适用于任何在等号右边的序列(译注:等号右操作数)。序列解包要求等号左边待赋值的变量数量与序列包含元素数目相同。注意多重赋值只是封装元组和序列解包的结合(译注:多重赋值:i, j = 1, 2

5.4 Set

Python也包含实现了集合的数据类型。集合是无序不重复的元素集。基本功能包括成员关系测试和重复实体消除。集合对象也支持并集,交集,差集以及对称差集等数学操作。

可以使用花括号和set()函数创建集合。谨记:创建空集合必须使用set函数,不能使用{},后者用于创建空字典。

以下是简单示范:

<code class="sourceCode python"><span class="op">>>></span> basket <span class="op">=</span> {<span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'orange'</span>, <span class="st">'banana'</span>}
<span class="op">>>></span> <span class="bu">print</span>(basket)                      <span class="co"># show that duplicates have been removed</span>
{<span class="st">'orange'</span>, <span class="st">'banana'</span>, <span class="st">'pear'</span>, <span class="st">'apple'</span>}
<span class="op">>>></span> <span class="st">'orange'</span> <span class="op">in</span> basket                 <span class="co"># fast membership testing</span>
<span class="va">True</span>
<span class="op">>>></span> <span class="st">'crabgrass'</span> <span class="op">in</span> basket
<span class="va">False</span>

<span class="op">>>></span> <span class="co"># Demonstrate set operations on unique letters from two words</span>
...
<span class="op">>>></span> a <span class="op">=</span> <span class="bu">set</span>(<span class="st">'abracadabra'</span>)
<span class="op">>>></span> b <span class="op">=</span> <span class="bu">set</span>(<span class="st">'alacazam'</span>)
<span class="op">>>></span> a                                  <span class="co"># unique letters in a</span>
{<span class="st">'a'</span>, <span class="st">'r'</span>, <span class="st">'b'</span>, <span class="st">'c'</span>, <span class="st">'d'</span>}
<span class="op">>>></span> a <span class="op">-</span> b                              <span class="co"># letters in a but not in b</span>
{<span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>}
<span class="op">>>></span> a <span class="op">|</span> b                              <span class="co"># letters in a or b or both</span>
{<span class="st">'a'</span>, <span class="st">'c'</span>, <span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>, <span class="st">'m'</span>, <span class="st">'z'</span>, <span class="st">'l'</span>}
<span class="op">>>></span> a <span class="op">&</span> b                              <span class="co"># letters in both a and b</span>
{<span class="st">'a'</span>, <span class="st">'c'</span>}
<span class="op">>>></span> a <span class="op">^</span> b                              <span class="co"># letters in a or b but not both</span>
{<span class="st">'r'</span>, <span class="st">'d'</span>, <span class="st">'b'</span>, <span class="st">'m'</span>, <span class="st">'z'</span>, <span class="st">'l'</span>}</code>

与列表推导式相同,Python也支持集合推导式:

<code class="sourceCode python"><span class="op">>>></span> a <span class="op">=</span> {x <span class="cf">for</span> x <span class="op">in</span> <span class="st">'abracadabra'</span> <span class="cf">if</span> x <span class="op">not</span> <span class="op">in</span> <span class="st">'abc'</span>}
<span class="op">>>></span> a
{<span class="st">'r'</span>, <span class="st">'d'</span>}</code>

5.5 Dictionaries

另一个内嵌入Python中的数据结构是字典(参见 Mapping Types - dict)。字典在其他一些语言中被称为“联合存储”或者“联合数组”。与序列不同,序列以一系列数字作索引,字典以作索引,键可以是任何不可变类型;通常使用字符串和数字作为键。只包含字符串,数字或者其他元组的元组也可以作为键;直接或者间接包含可变对象的元组不能作为键。因为列表可以使用索引赋值,切片赋值或者append()以及extend()等方法改变自身,所以列表不能作为键。

最好的理解字典的方式是将其认为是键值对的无序集合,同一集合中键唯一。一对花括号创建空字典:{}。在花括号中放置由逗号分隔键值对列表可以为字典添加初始键值对;这也是字典输出的格式。

字典提供的主要操作是:使用键存储值以及取值。可以使用del删除一个键值对。如果使用已经存在的键来存储值,那么与键关联的旧值会被重写。使用不存在的键来取值会抛出异常。

在字典上执行list(d.keys())返回字典所有键的无序列表(使用sorted(d.keys())使其有序)[2]。使用关键字in检查键在字典中是否存在。

以下是使用字典的示例:

<code class="sourceCode python"><span class="op">>>></span> tel <span class="op">=</span> {<span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'sape'</span>: <span class="dv">4139</span>}
<span class="op">>>></span> tel[<span class="st">'guido'</span>] <span class="op">=</span> <span class="dv">4127</span>
<span class="op">>>></span> tel
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>}
<span class="op">>>></span> tel[<span class="st">'jack'</span>]
<span class="dv">4098</span>
<span class="op">>>></span> <span class="kw">del</span> tel[<span class="st">'sape'</span>]
<span class="op">>>></span> tel[<span class="st">'irv'</span>] <span class="op">=</span> <span class="dv">4127</span>
<span class="op">>>></span> tel
{<span class="st">'guido'</span>: <span class="dv">4127</span>, <span class="st">'irv'</span>: <span class="dv">4127</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>}
<span class="op">>>></span> <span class="bu">list</span>(tel.keys())
[<span class="st">'irv'</span>, <span class="st">'guido'</span>, <span class="st">'jack'</span>]
<span class="op">>>></span> <span class="bu">sorted</span>(tel.keys())
[<span class="st">'guido'</span>, <span class="st">'irv'</span>, <span class="st">'jack'</span>]
<span class="op">>>></span> <span class="st">'guido'</span> <span class="op">in</span> tel
<span class="va">True</span>
<span class="op">>>></span> <span class="st">'jack'</span> <span class="op">not</span> <span class="op">in</span> tel
<span class="va">False</span></code>

dict()构造器直接使用键值对序列构造字典:

<code class="sourceCode python"><span class="op">>>></span> <span class="bu">dict</span>([(<span class="st">'sape'</span>, <span class="dv">4139</span>), (<span class="st">'guido'</span>, <span class="dv">4127</span>), (<span class="st">'jack'</span>, <span class="dv">4098</span>)])
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>}</code>

此外,字典推导式可以从任意键值表达式中创建字典:

<code class="sourceCode python"><span class="op">>>></span> {x: x<span class="op">**</span><span class="dv">2</span> <span class="cf">for</span> x <span class="op">in</span> (<span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">6</span>)}
{<span class="dv">2</span>: <span class="dv">4</span>, <span class="dv">4</span>: <span class="dv">16</span>, <span class="dv">6</span>: <span class="dv">36</span>}</code>

当键是简单的字符串时,可以使用关键字参数来指定键值对:

<code class="sourceCode python"><span class="op">>>></span> <span class="bu">dict</span>(sape<span class="op">=</span><span class="dv">4139</span>, guido<span class="op">=</span><span class="dv">4127</span>, jack<span class="op">=</span><span class="dv">4098</span>)
{<span class="st">'sape'</span>: <span class="dv">4139</span>, <span class="st">'jack'</span>: <span class="dv">4098</span>, <span class="st">'guido'</span>: <span class="dv">4127</span>}</code>

5.6 Looping Techniques

遍历字典时,使用items()方法可以同时检索键及其对应的值。

<code class="sourceCode python"><span class="op">>>></span> knights <span class="op">=</span> {<span class="st">'gallahad'</span>: <span class="st">'the pure'</span>, <span class="st">'robin'</span>: <span class="st">'the brave'</span>}
<span class="op">>>></span> <span class="cf">for</span> k, v <span class="op">in</span> knights.items():
...     <span class="bu">print</span>(k, v)
...
gallahad the pure
robin the brave</code>

遍历序列时,使用enumerate()函数可以同时检索位置索引及其对应的值:

<code class="sourceCode python"><span class="op">>>></span> <span class="cf">for</span> i, v <span class="op">in</span> <span class="bu">enumerate</span>([<span class="st">'tic'</span>, <span class="st">'tac'</span>, <span class="st">'toe'</span>]):
...     <span class="bu">print</span>(i, v)
...
<span class="dv">0</span> tic
<span class="dv">1</span> tac
<span class="dv">2</span> toe</code>

同时遍历两个或者更多序列时,使用zip()函数可以将元素组成对:

<code class="sourceCode python"><span class="op">>>></span> questions <span class="op">=</span> [<span class="st">'name'</span>, <span class="st">'quest'</span>, <span class="st">'favorite color'</span>]
<span class="op">>>></span> answers <span class="op">=</span> [<span class="st">'lancelot'</span>, <span class="st">'the holy grail'</span>, <span class="st">'blue'</span>]
<span class="op">>>></span> <span class="cf">for</span> q, a <span class="op">in</span> <span class="bu">zip</span>(questions, answers):
...     <span class="bu">print</span>(<span class="st">'What is your </span><span class="sc">{0}</span><span class="st">?  It is </span><span class="sc">{1}</span><span class="st">.'</span>.<span class="bu">format</span>(q, a))
...
What <span class="op">is</span> your name?  It <span class="op">is</span> lancelot.
What <span class="op">is</span> your quest?  It <span class="op">is</span> the holy grail.
What <span class="op">is</span> your favorite color?  It <span class="op">is</span> blue.</code>

需要逆序遍历序列时,首先指定一个正向的序列,然后调用reversed()函数:

<code class="sourceCode python"><span class="op">>>></span> <span class="cf">for</span> i <span class="op">in</span> <span class="bu">reversed</span>(<span class="bu">range</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">2</span>)):
...     <span class="bu">print</span>(i)
...
<span class="dv">9</span>
<span class="dv">7</span>
<span class="dv">5</span>
<span class="dv">3</span>
<span class="dv">1</span></code>

需要以特定顺序遍历序列时,使用sorted()函数返回新的有序序列,原序列不会改动:

<code class="sourceCode python"><span class="op">>>></span> basket <span class="op">=</span> [<span class="st">'apple'</span>, <span class="st">'orange'</span>, <span class="st">'apple'</span>, <span class="st">'pear'</span>, <span class="st">'orange'</span>, <span class="st">'banana'</span>]
<span class="op">>>></span> <span class="cf">for</span> f <span class="op">in</span> <span class="bu">sorted</span>(<span class="bu">set</span>(basket)):
...     <span class="bu">print</span>(f)
...
apple
banana
orange
pear</code>

有时需要在遍历序列的同时修改序列,创建新的替代序列更加简单并且安全:

<code class="sourceCode python"><span class="op">>>></span> <span class="im">import</span> math
<span class="op">>>></span> raw_data <span class="op">=</span> [<span class="fl">56.2</span>, <span class="bu">float</span>(<span class="st">'NaN'</span>), <span class="fl">51.7</span>, <span class="fl">55.3</span>, <span class="fl">52.5</span>, <span class="bu">float</span>(<span class="st">'NaN'</span>), <span class="fl">47.8</span>]
<span class="op">>>></span> filtered_data <span class="op">=</span> []
<span class="op">>>></span> <span class="cf">for</span> value <span class="op">in</span> raw_data:
...     <span class="cf">if</span> <span class="op">not</span> math.isnan(value):
...         filtered_data.append(value)
...
<span class="op">>>></span> filtered_data
[<span class="fl">56.2</span>, <span class="fl">51.7</span>, <span class="fl">55.3</span>, <span class="fl">52.5</span>, <span class="fl">47.8</span>]</code>

5.7 More on Conditions

whileif语句中使用的条件可以包含任意操作符,不仅仅是比较运算符。

比较运算符innot in检查指定值在序列中是否存在(不存在)。操作符isis not比较两个对象是否真正相同(内存地址比较);这两个操作符只对像列表那样的可变对象重要。所有的比较运算符拥有相同的优先级,并且都低于数字运算符。

比较运算符可以链接起来。例如a 测试<code>b是否大于a同时b等于c(译注:同其他高级语言的:a )

比较运算符可以结合布尔运算符andor使用,比较的结果(或者其他任何布尔表达式)可以使用not来作否定。and,ornot优先级比比较运算符低;其中not的优先级最高而or优先级最低,因此A and not B or C等同于(A and (not B)) or C。一如既往,可以使用圆括号表述想要的优先级顺序。

布尔运算符andor号称短路运算符:它们的参数从左向右求值,一旦结果确定,求值过程就会停止。例如,如果AC是真,B是假,A and B and C不会对表达式C求值(译注:A and B为假,已经确定了整个表达式A and B and C的值为假,表达式C的值对结果不会造成影响,因此不会对其求值)。当用作一般值而不是布尔值时,短路操作的返回值是最后一个求值的参数。

可以将比较运算或者布尔表达式赋值给变量:

<code class="sourceCode python"><span class="op">>>></span> string1, string2, string3 <span class="op">=</span> <span class="st">''</span>, <span class="st">'Trondheim'</span>, <span class="st">'Hammer Dance'</span>
<span class="op">>>></span> non_null <span class="op">=</span> string1 <span class="op">or</span> string2 <span class="op">or</span> string3
<span class="op">>>></span> non_null
<span class="co">'Trondheim'</span></code>

注意在Python中,赋值操作不能像C语言一样在表达式内发生。C程序员也许会抱怨,但是这避免了C程序中遇到的一个普遍问题:当想要表示==时候可能误用了=

5.8 Comparing Sequences and Other Types

相同序列类型之间的序列对象可以相互比较。比较使用字典序:首先比较两个序列的第一项,如果它们不同,比较运算的结果就可确定了;如果它们不同,比较两个序列中的下一个项,以此类推,直到其中一个序列耗尽。如果被比较的两个项是同一类型的,那么使用字典序递归比较。如果两个序列的所有项都是相等的,那么他们相等。如果其中一个序列是另一个序列的子序列,那么短的一个序列较小。字符串的字典序使用Unicode代码点数字排序单个字符。

以下是相同类型的序列对象之间的比较示例:

<code class="sourceCode python">(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>)              <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)
[<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]              <span class="op"><</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>]
<span class="co">'ABC'</span> <span class="op"><</span> <span class="st">'C'</span> <span class="op"><</span> <span class="st">'Pascal'</span> <span class="op"><</span> <span class="st">'Python'</span>
(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>)           <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">4</span>)
(<span class="dv">1</span>, <span class="dv">2</span>)                 <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, <span class="op">-</span><span class="dv">1</span>)
(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>)             <span class="op">==</span> (<span class="fl">1.0</span>, <span class="fl">2.0</span>, <span class="fl">3.0</span>)
(<span class="dv">1</span>, <span class="dv">2</span>, (<span class="st">'aa'</span>, <span class="st">'ab'</span>))   <span class="op"><</span> (<span class="dv">1</span>, <span class="dv">2</span>, (<span class="st">'abc'</span>, <span class="st">'a'</span>), <span class="dv">4</span>)</code>

注意当不同类型对象之间有合适的比较方式时,使用或者<code>>比较不同类型的对象是合法的。例如,混合数字类型之间的比较是根据其数字上的值,0等于0.0。否则,解释器会抛出TypeException异常,而不是随意提供结果

Footnotes

[1] 其他语言可能返回改变后的对象,从而允许方法链接,如:d->insert("a")>remove("b")->sort();
[2] 调用d.keys()返回一个dictionary view对象。从而支持如成员关系测试和迭代之类的操作,但是其内容并不是独立于原始字典的,只是一个视图

The above is the detailed content of [译]The Python Tutorial#Data Structures. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.