码迷,mamicode.com
首页 > 编程语言 > 详细

Python数据结构(二)

时间:2015-03-15 23:31:23      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:

5.2. The del statement

There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice). For example:

del语句:删除列表中指定位置的元素。与列表的pop()方法有点不同的是: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
[]

del can also be used to delete entire variables:

del还可以删除整个列表变量:

>>> del a

Referencing the name a hereafter is an error (at least until another value is assigned to it). We’ll find other uses for del later.

现在引用a变量,则会出现错误。我们将会在后面看到del另外的作用。

5.3. Tuples and Sequences

We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of sequence data types (see Sequence Types — list, tuple, range). Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.

我们已经看到列表和字符串有许多共同的属性,例如索引和切片操作。他们只是序列类型的两个例子(参考 序列类型--列表,元组,range)。因为Python正不断的处于更新中,其他序列类型可能会再被加进来。元组:另一个标准的序列数据类型。

A tuple consists of a number of values separated by commas, for instance:

元组是由一些由逗号分隔的元素组成,例如:

>>> 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] = 88888
Traceback (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])

As you see, on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression). It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.

正如你所看见的,元组的输出总是用圆括号括起来,因此嵌套的元组可以很好的被解释器所识别;尽管元组常常需要加入圆括号(如果元组是一个较大表达式的一部分),但元组的输入可以加圆括号,也可不加。不可以给元组的某个元素再赋值,然而,我们可以在创建元组的时候包含可变对象,比如列表。

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

尽管元组看起来和列表很相似,但他们常常用于不同的情形和目的。元组是不可变对象,通常包含不同类型的元素,并通过unpacking(解包)或索引访问。列表是可变量,它的元素通常是同一种类型,并通过迭代对象访问整个列表。

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:

构造一个包含0个或1个元素的元组可能看上去有点特别:语法上有点怪异。空元组通常就是用一对空圆括号表示;包含一个元素的元组需要在该元素的后面加上一个逗号(对于单个元素的元组,圆括号可有可无).看起来不够优雅,但是很有效。例如:

>>> empty = ()
>>> singleton = hello,    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
(hello,)

The statement t = 12345, 54321, ‘hello!‘ is an example of tuple packing: the values 12345, 54321 and ‘hello!‘ are packed together in a tuple. The reverse operation is also possible:

语句 t 12345, 54321, ‘hello!‘ 是一个元组打包的例子:元素1234554321 and ‘hello!‘被元组包裹在一起。相反的操作也是合法的:

>>> x, y, z = t

This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

这个序列的解包调用例子,刚好合适。其他序列都可以放到等号右边作解包操作。序列的解包操作要求等号左边的变量和序列右边的元素个数要相同。注意,多重赋值仅仅只是元组的一个打包和解包的一个结合。

5.4. Sets

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Python还包含集合这种数据类型。一个集合是一个无序的、没有重复元素的容器。集合最基本的用途是:检查元素是否在集合中,以及排除重复元素。集合对象支持数学运算操作,例如:联合、交集、difference(非?)、symmetric difference。

Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

可以使用花括号或set()函数来创建一个集合对象。注意:创建一个空的集合对象必须使用set(),不能使用{};我们将会在下一节讨论如何用{}创建一个空的字典。

Here is a brief demonstration:

举几个例子:

>>> basket = {apple, orange, apple, pear, orange, banana}
>>> print(basket)                      # show that duplicates have been removed
{orange, banana, pear, apple}
>>> orange in basket                 # fast membership testing
True
>>> crabgrass in basket
False

>>> # 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 either a or b
{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}

Similarly to list comprehensions, set comprehensions are also supported:

类似于 list comprehensions。集合也支持 set comprehensions:

>>> a = {x for x in abracadabra if x not in abc}
>>> a
{r, d}

5.5. Dictionaries

Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().

另一个Python内置的有用的数据类型是:字典。字典在其他编程语言中可能被称为:"associative memories" 或者 "associative arrays"。与序列类型不同的是:字典是通过键来索引的,键必须是不可变类型; 键一般常常是字符串或数值类型。如果元组仅仅只包含字符串、数值或元组,则元组也可以当作键; 如果元组中包含可变对象,不管是直接还是间接包含都不能作为索引。你不能把列表当作键,因为列表是可变对象。

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

我们可以把字典看作是无序的键:值 这样的集合(键必须是唯一的)。可以使用一对花括号创建一个空的字典{}。字典中键和值用分号隔开,每对键值用逗号隔开。

The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.

字典的主要操作就是:存储键值对,必要的时候再取出来。使用del 语句可以很容易的删除键值对。如果你使用了一个已经存储了的键,那么久的键值对就会被取代。使用一个没存在的键提取值则会报错。

Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys()) instead). [2] To check whether a single key is in the dictionary, use the in keyword.

执行list(d.keys())操作,d是一个字典,则返回所有的键并存储在列表中,他们在列表中的顺序是无序的(如果你想排序,可以使用sorted(d.keys()))。可以使用关键字in来检查某个键是否在字典中存在。

这里举一个使用字典的例子:

>>> tel = {jack: 4098, sape: 4139}
>>> tel[guido] = 4127
>>> tel
{sape: 4139, guido: 4127, jack: 4098}
>>> tel[jack]
4098
>>> del tel[sape]
>>> tel[irv] = 4127
>>> tel
{guido: 4127, irv: 4127, jack: 4098}
>>> list(tel.keys())
[irv, guido, jack]
>>> sorted(tel.keys())
[guido, irv, jack]
>>> guido in tel
True
>>> jack not in tel
False

 The dict() constructor builds dictionaries directly from sequences of key-value pairs:

dict()函数可以直接从一个包含键值对的序列中创建一个字典。

>>> dict([(sape, 4139), (guido, 4127), (jack, 4098)])
{sape: 4139, jack: 4098, guido: 4127}

 

 In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:

另外,dict comprehensions也能创建任意个键值对的字典:

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

 

 When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:

如果键是简单的字符串,我们可以使用关键字参数指定键值对:

>>> dict(sape=4139, guido=4127, jack=4098)
{sape: 4139, jack: 4098, guido: 4127}

 

5.6. Looping Techniques

 When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.

当遍历字典的时候,可以使用items()函数同时提取键和值。

>>> knights = {gallahad: the pure, robin: the brave}
>>> for k, v in knights.items():
...     print(k, v)
...
gallahad the pure
robin the brave

 

When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.

当遍历序列的时候 。可以使用enumerate()函数同时提取索引和对应的值。

>>> for i, v in enumerate([tic, tac, toe]):
...     print(i, v)
...
0 tic
1 tac
2 toe

 

 To loop over two or more sequences at the same time, the entries can be paired with the zip() function.

当同时遍历两个序列时。可以使用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.

To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.

为了变量反转之后的序列,可以调用reversed()函数。

>>> for i in reversed(range(1, 10, 2)):
...     print(i)
...
9
7
5
3
1

To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.

想要有序的遍历序列,可以使用函数sorted()。该函数返回一个有序的列表,但元素在原序列中的顺序是不变的。

>>> basket = [apple, orange, apple, pear, orange, banana]
>>> for f in sorted(set(basket)):
...     print(f)
...
apple
banana
orange
pear

To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy. Looping over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:

如果在遍历序列的时候想修改序列,建议先复制该序列。因为变量序列的时候不会显示的去复制。切片操作(复制一个序列)非常适合这种情况。

>>> words = [cat, window, defenestrate]
>>> for w in words[:]:  # Loop over a slice copy of the entire list.
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
[defenestrate, cat, window, defenestrate]

5.7. More on Conditions

The conditions used in while and if statements can contain any operators, not just comparisons.

while和if语句可以包含任何操作返回的值,而不仅仅是比较操作的条件判断

The comparison operators in and not in check whether a value occurs (does not occur) in a sequence. The operators is and is not compare whether two objects are really the same object; this only matters for mutable objects like lists. All comparison operators have the same priority, which is lower than that of all numerical operators.

in和not in检查值是否在序列中。is 和 is not 比较两个对象是不是真正的为同一个对象;这仅仅适用于可变对象,例如list(不可变对象可以看着是值传递相关的操作,可变对象可以看着是引用传递的操作)。所有的比较操作都有相同的优先级,比较操作优先级低于所有的数值运算。

Comparisons can be chained. For example, a < b == c tests whether a is less than b and moreover b equals c.

能够链式比较。例如: a < b == c 检查 a 是否小于b 并且 b 是否等于 c。

Comparisons may be combined using the Boolean operators and and or, and the outcome of a comparison (or of any other Boolean expression) may be negated with not. These have lower priorities than comparison operators; between them, not has the highest priority and or the lowest, so that A and not B or C is equivalent to (A and (not B)) or C. As always, parentheses can be used to express the desired composition.

比较操作可以使用布尔操作符:and 和 or 否定可以使用not。他们的优先级低于比较操作符;他们中,not 具有最高的优先级,or最低。因此 A and not B or C 相当于 (A and (not B)) or C。因此尽量养成加括号的习惯。

The Boolean operators and and or are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if A and C are true but B is false, A and B and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.

布尔运算符and 和 or 有时被称为短路操作符:从左至右依次判断结果,当得到确定的结果后,则后面的比较就不再执行了,例如:如果 A和C为真,B为假,A and B and C将不会执行C操作。当使用其他比较不使用布尔,则每个表达式都会被执行。

It is possible to assign the result of a comparison or other Boolean expression to a variable. For example,

很容易将比较操作或布尔表达式赋值给某个变量。例如:

>>> string1, string2, string3 = ‘‘, Trondheim, Hammer Dance
>>> non_null = string1 or string2 or string3
>>> non_null
Trondheim

 

Note that in Python, unlike C, assignment cannot occur inside expressions. C programmers may grumble about this, but it avoids a common class of problems encountered in C programs: typing = in an expression when == was intended.

与C语言不通,在Python中,不能在表达式里面赋值,C程序员可能觉得不可思议,但是这样能避免在C语言中常见的一个错误:本来想书写==却写了=。

5.8. Comparing Sequences and Other Types

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type:

相同序列类型的两个序列对象也可以做比较操作。他们使用逻辑顺序比较:先是两个序列的第一个元素比较,接着是两个序列的第二个元素比较...直到知道结果后,则退出比较。如果两个序列的所有元素都相等,那么这两个序列就相等,如果一个序列是另一个序列的子序列,那么这个子序列就小于父序列。字符串的比较是使用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)

Note that comparing objects of different types with < or > is legal provided that the objects have appropriate comparison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a TypeError exception.

注意:不同对象之间也可以用 < 或者 > 进行比较,但被比较的对象具有合适的比较方法。例如,混合的数值类型通过值进行比较,因此 0 等于0.0。如果拥有的比较方法不合适,则会抛出TypeError异常。

 

Python数据结构(二)

标签:

原文地址:http://www.cnblogs.com/lijie-vs/p/4340727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!