Subsections


5. Êý¾Ý½á¹¹ Data Structures

This chapter describes some things you've learned about already in more detail, and adds some new things as well.

±¾Õ½ÚÉîÈë½²ÊöһЩÄãÒѾ­Ñ§Ï°¹ýµÄ¶«Î÷£¬²¢ÇÒ»¹¼ÓÈëÁËеÄÄÚÈÝ¡£


5.1 ÉîÈëÁ´±í More on Lists

The list data type has some more methods. Here are all of the methods of list objects:

Á´±íÀàÐÍÓкܶ෽·¨£¬ÕâÀïÊÇÁ´±íÀàÐ͵ÄËùÓз½·¨£º

append( x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].

°ÑÒ»¸öÔªËØÌí¼Óµ½Á´±íµÄ½á⣬Ï൱ÓÚ a[len(a):] = [x]

extend( L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.

ͨ¹ýÌí¼ÓÖ¸¶¨Á´±íµÄËùÓÐÔªËØÀ´À©³äÁ´±í£¬Ï൱ÓÚ a[len(a):] = L¡£

insert( i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

ÔÚÖ¸¶¨Î»ÖòåÈëÒ»¸öÔªËØ¡£µÚÒ»¸ö²ÎÊýÊÇ×¼±¸²åÈëµ½ÆäÇ°ÃæµÄÄǸöÔªËصÄË÷Òý£¬ÀýÈça.insert(0, x) »á²åÈëµ½Õû¸öÁ´±í֮ǰ£¬¶øa.insert(len(a), x) Ï൱ÓÚ a.append(x)¡£

remove( x)
Remove the first item from the list whose value is x. It is an error if there is no such item.

ɾ³ýÁ´±íÖÐֵΪxµÄµÚÒ»¸öÔªËØ¡£Èç¹ûûÓÐÕâÑùµÄÔªËØ£¬¾Í»á·µ»ØÒ»¸ö´íÎó¡£

pop( [i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() returns the last item in the list. The item is also removed from the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

´ÓÁ´±íµÄÖ¸¶¨Î»ÖÃɾ³ýÔªËØ£¬²¢½«Æä·µ»Ø¡£Èç¹ûûÓÐÖ¸¶¨Ë÷Òý£¬a.pop()·µ»Ø×îºóÒ»¸öÔªËØ¡£ÔªËØËæ¼´´ÓÁ´±íÖб»É¾³ý¡££¨·½·¨ÖÐiÁ½±ßµÄ·½À¨ºÅ±íʾÕâ¸ö²ÎÊýÊÇ¿ÉÑ¡µÄ£¬¶ø²»ÊÇÒªÇóÄãÊäÈëÒ»¶Ô·½À¨ºÅ£¬Äã»á¾­³£ÔÚPython ¿â²Î¿¼ÊÖ²áÖÐÓöµ½ÕâÑùµÄ±ê¼Ç¡££©

index( x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.

·µ»ØÁ´±íÖеÚÒ»¸öֵΪxµÄÔªËصÄË÷Òý¡£Èç¹ûûÓÐÆ¥ÅäµÄÔªËؾͻ᷵»ØÒ»¸ö´íÎó¡£

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

·µ»ØxÔÚÁ´±íÖгöÏֵĴÎÊý¡£

sort( )
Sort the items of the list, in place.

¶ÔÁ´±íÖеÄÔªËؽøÐÐÊʵ±µÄÅÅÐò¡£

reverse( )
Reverse the elements of the list, in place.

µ¹ÅÅÁ´±íÖеÄÔªËØ¡£

An example that uses most of the list methods:

ÏÂÃæÕâ¸öʾÀýÑÝʾÁËÁ´±íµÄ´ó²¿·Ö·½·¨£º

>>> a = [66.6, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.6), a.count('x')
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.6, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.6, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.6]
>>> a.sort()
>>> a
[-1, 1, 66.6, 333, 333, 1234.5]


5.1.1 °ÑÁ´±íµ±×÷¶ÑջʹÓà Using Lists as Stacks

The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (``last-in, first-out''). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. For example:

Á´±í·½·¨Ê¹µÃÁ´±í¿ÉÒԺܷ½±ãµÄ×öΪһ¸ö¶ÑÕ»À´Ê¹Ó㬶ÑÕ»×÷ΪÌض¨µÄÊý¾Ý½á¹¹£¬×îÏȽøÈëµÄÔªËØ×îºóÒ»¸ö±»ÊÍ·Å£¨ºó½øÏȳö£©¡£ÓÃappend() ·½·¨¿ÉÒÔ°ÑÒ»¸öÔªËØÌí¼Óµ½¶ÑÕ»¶¥¡£Óò»Ö¸¶¨Ë÷ÒýµÄpop() ·½·¨¿ÉÒÔ°ÑÒ»¸öÔªËØ´Ó¶ÑÕ»¶¥ÊͷųöÀ´¡£ÀýÈ磺

>>> 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 °ÑÁ´±íµ±×÷¶ÓÁÐʹÓà Using Lists as Queues

You can also use a list conveniently as a queue, where the first element added is the first element retrieved (``first-in, first-out''). To add an item to the back of the queue, use append(). To retrieve an item from the front of the queue, use pop() with 0 as the index. For example:

ÄãÒ²¿ÉÒÔ°ÑÁ´±íµ±×ö¶ÓÁÐʹÓ㬶ÓÁÐ×÷ΪÌض¨µÄÊý¾Ý½á¹¹£¬×îÏȽøÈëµÄÔªËØ×îÏÈÊÍ·Å£¨ÏȽøÏȳö£©¡£Ê¹Óà append()·½·¨¿ÉÒÔ°ÑÔªËØÌí¼Óµ½¶ÓÁÐ×îºó£¬ÒÔ0Ϊ²ÎÊýµ÷Óà pop() ·½·¨¿ÉÒÔ°Ñ×îÏȽøÈëµÄÔªËØÊͷųöÀ´¡£ÀýÈ磺

>>> queue = ["Eric", "John", "Michael"]
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.pop(0)
'Eric'
>>> queue.pop(0)
'John'
>>> queue
['Michael', 'Terry', 'Graham']


5.1.3 º¯Êý»¯±à³Ì¹¤¾ß Functional Programming Tools

There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().

¶ÔÓÚÁ´±íÀ´½²£¬ÓÐÈý¸öÄÚÖú¯Êý·Ç³£ÓÐÓãºfilter()£¬ map()£¬ ºÍ reduce()¡£

"filter(function, sequence)" returns a sequence (of the same type, if possible) consisting of those items from the sequence for which function(item) is true. For example, to compute some primes:

"filter(function, sequence)"·µ»ØÒ»¸öÐòÁУ¨sequence£©£¬°üÀ¨Á˸ø¶¨ÐòÁÐÖÐËùÓе÷ÓÃfunction(item)ºó·µ»ØֵΪtrueµÄÔªËØ¡££¨Èç¹û¿ÉÄܵĻ°£¬»á·µ»ØÏàͬµÄÀàÐÍ£©¡£ÀýÈ磬ÒÔϳÌÐò¿ÉÒÔ¼ÆË㲿·ÖËØÊý£º

>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

"map(function, sequence)" calls function(item) for each of the sequence's items and returns a list of the return values. For example, to compute some cubes:

"map(function, sequence)" Ϊÿһ¸öÔªËØÒÀ´Îµ÷ÓÃfunction(item)²¢½«·µ»ØÖµ×é³ÉÒ»¸öÁ´±í·µ»Ø¡£ÀýÈ磬ÒÔϳÌÐò¼ÆËãÁ¢·½£º

>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). For example:

¿ÉÒÔ´«Èë¶à¸öÐòÁУ¬º¯ÊýÒ²±ØÐëÒªÓжÔÓ¦ÊýÁ¿µÄ²ÎÊý£¬Ö´ÐÐʱ»áÒÀ´ÎÓø÷ÐòÁÐÉ϶ÔÓ¦µÄÔªËØÀ´µ÷Óú¯Êý£¨Èç¹ûijЩÐòÁбÈÆäËüµÄ¶Ì£¬¾ÍÓÃNoneÀ´´úÌ棩¡£Èç¹û°ÑNone×öΪһ¸öº¯Êý´«È룬ÔòÖ±½Ó·µ»Ø²ÎÊý×öΪÌæ´ú¡£ÀýÈ磺

>>> seq = range(8)
>>> def add(x, y): return x+y
...
>>> map(add, seq, seq)
[0, 2, 4, 6, 8, 10, 12, 14]

"reduce(func, sequence)" returns a single value constructed by calling the binary function func on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:

"reduce(func, sequence)" ·µ»ØÒ»¸öµ¥Öµ£¬ËüÊÇÕâÑù¹¹ÔìµÄ£ºÊ×ÏÈÒÔÐòÁеÄÇ°Á½¸öÔªËص÷Óú¯Êý£¬ÔÙÒÔ·µ»ØÖµºÍµÚÈý¸ö²ÎÊýµ÷Óã¬ÒÀ´ÎÖ´ÐÐÏÂÈ¥¡£ÀýÈ磬ÒÔϳÌÐò¼ÆËã1µ½10µÄÕûÊýÖ®ºÍ£º

>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55

If there's only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.

Èç¹ûÐòÁÐÖÐÖ»ÓÐÒ»¸öÔªËØ£¬¾Í·µ»ØËü£¬Èç¹ûÐòÁÐÊǿյģ¬¾ÍÅ׳öÒ»¸öÒì³£¡£

A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on. For example,

¿ÉÒÔ´«ÈëµÚÈý¸ö²ÎÊý×öΪ³õʼֵ¡£Èç¹ûÐòÁÐÊǿյģ¬¾Í·µ»Ø³õʼֵ£¬·ñÔòº¯Êý»áÏȽÓÊÕ³õʼֵºÍÐòÁеĵÚÒ»¸öÔªËØ£¬È»ºóÊÇ·µ»ØÖµºÍÏÂÒ»¸öÔªËØ£¬ÒÀ´ËÀàÍÆ¡£ÀýÈ磺

>>> def sum(seq):
...     def add(x,y): return x+y
...     return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0

Don't use this example's definition of sum(): since summing numbers is such a common need, a built-in function sum(sequence) is already provided, and works exactly like this. New in version 2.3.

²»ÒªÏñʾÀýÖÐÕâÑù¶¨Òåsum()£ºÒòΪºÏ¼ÆÊýÖµÊÇÒ»¸öͨÓõÄÐèÇó£¬ÔÚ2.3°æÖУ¬ÌṩÁËÄÚÖõÄsum(sequence) º¯Êý¡£ New in version 2.3.

5.1.4 Á´±íÍƵ¼Ê½ List Comprehensions

List comprehensions provide a concise way to create lists without resorting to use of map(), filter() and/or lambda. The resulting list definition tends often to be clearer than lists built using those constructs. Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses. The result will be a list resulting from evaluating the expression in the context of the for and if clauses which follow it. If the expression would evaluate to a tuple, it must be parenthesized.

Á´±íÍƵ¼Ê½ÌṩÁËÒ»¸ö´´½¨Á´±íµÄ¼òµ¥Í¾¾¶£¬ÎÞÐèʹÓÃmap()£¬ filter() ÒÔ¼° lambda¡£·µ»ØÁ´±íµÄ¶¨Òåͨ³£Òª±È´´½¨ÕâЩÁ´±í¸üÇåÎú¡£Ã¿Ò»¸öÁ´±íÍƵ¼Ê½°üÀ¨ÔÚÒ»¸öfor Óï¾äÖ®ºóµÄ±í´ïʽ£¬Áã»ò¶à¸ö for»ò if Óï¾ä¡£·µ»ØÖµÊÇÓÉ for »ò if×Ó¾äÖ®ºóµÄ±í´ïʽµÃµ½µÄÔªËØ×é³ÉµÄÁ´±í¡£Èç¹ûÏëÒªµÃµ½Ò»¸öÔª×飬±ØÐëÒª¼ÓÉÏÀ¨ºÅ¡£

>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec]  # error - parens required for tuples
  File "<stdin>", line 1, in ?
    [x, x**2 for x in vec]
               ^
SyntaxError: invalid syntax
>>> [(x, x**2) for x in vec]
[(2, 4), (4, 16), (6, 36)]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x*y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]
>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
>>> [vec1[i]*vec2[i] for i in range(len(vec1))]
[8, 12, -54]

List comprehensions are much more flexible than map() and can be applied to functions with more than one argument and to nested functions:

Á´±íÍƵ¼Ê½±È map()¸ü¸´ÔÓ£¬¿Éµ÷Óöà¸ö²ÎÊýºÍǶÌ׺¯Êý¡£

>>> [str(round(355/113.0, i)) for i in range(1,6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']


5.2 del Óï¾ä

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

ÓÐÒ»¸ö·½·¨¿É´ÓÁ´±íÖÐɾ³ýÖ¸¶¨Ë÷ÒýµÄÔªËØ£ºdel Óï¾ä¡£Õâ¸ö·½·¨Ò²¿ÉÒÔ´ÓÁ´±íÖÐɾ³ýÇÐƬ£¨Ö®Ç°ÎÒÃÇÊÇ°ÑÒ»¸ö¿ÕÁ´±í¸³¸øÇÐƬ£©¡£ÀýÈ磺

>>> a = [-1, 1, 66.6, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.6, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.6, 1234.5]

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.

´ËºóÔÙÒýÓÃÕâ¸öÃû×ֻᷢÉú´íÎó£¨ÖÁÉÙÒªµ½¸øËü¸³ÁíÒ»¸öֵΪֹ£©¡£ºóÃæÎÒÃÇ»¹»á·¢ÏÖdelµÄÆäËüÓ÷¨¡£


5.3 Ôª×飨Tuples£©ºÍÐòÁУ¨Sequences £©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. Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the tuple.

ÎÒÃÇÖªµÀÁ´±íºÍ×Ö·û´®ÓкܶàͨÓõÄÊôÐÔ£¬ÀýÈçË÷ÒýºÍÇÐƬ²Ù×÷¡£ËüÃÇÊÇÐòÁÐÀàÐÍÖеÄÁ½ÖÖ¡£ÒòΪ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))

As you see, on output tuples are alway 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).

ÈçÄãËù¼û£¬Ôª×éÔÚÊä³öʱ×ÜÊÇÓÐÀ¨ºÅµÄ£¬ÒÔ±ãÓÚÕýÈ·±í´ïǶÌ׽ṹ¡£ÔÚÊäÈëʱ¿ÉÄÜÓлòûÓÐÀ¨ºÅ¶¼¿ÉÒÔ£¬²»¹ý¾­³£À¨ºÅ¶¼ÊDZØÐëµÄ£¨Èç¹ûÔª×éÊÇÒ»¸ö¸ü´óµÄ±í´ïʽµÄÒ»²¿·Ö£©¡£

Tuples have many uses. For example: (x, y) coordinate pairs, employee records from a database, etc. Tuples, like strings, are immutable: it is not possible to assign to the individual items of a tuple (you can simulate much of the same effect with slicing and concatenation, though). It is also possible to create tuples which contain mutable objects, such as lists.

Ôª×éÓкܶàÓÃ;¡£ÀýÈç(x, y)×ø±êµã£¬Êý¾Ý¿âÖеÄÔ±¹¤¼Ç¼µÈµÈ¡£Ôª×é¾ÍÏñ×Ö·û´®£¬²»¿É¸Ä±ä£º²»ÄܸøÔª×éµÄÒ»¸ö¶ÀÁ¢µÄÔªËظ³Öµ£¨¾¡¹ÜÄã¿ÉÒÔͨ¹ýÁª½ÓºÍÇÐƬÀ´Ä£·Â£©¡£Ò²¿ÉÒÔͨ¹ý°üº¬¿É±ä¶ÔÏóÀ´´´½¨Ôª×飬ÀýÈçÁ´±í¡£

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:

Ò»¸öÌØÊâµÄÎÊÌâÊǹ¹Ôì°üº¬Áã¸ö»òÒ»¸öÔªËصÄÔª×飺ΪÁËÊÊÓ¦ÕâÖÖÇé¿ö£¬Óï·¨ÉÏÓÐһЩ¶îÍâµÄ¸Ä±ä¡£Ò»¶Ô¿ÕµÄÀ¨ºÅ¿ÉÒÔ´´½¨¿ÕÔª×飻Ҫ´´½¨Ò»¸öµ¥ÔªËØÔª×é¿ÉÒÔÔÚÖµºóÃæ¸úÒ»¸ö¶ººÅ£¨ÔÚÀ¨ºÅÖзÅÈëÒ»¸öµ¥ÖµÊDz»¹»µÄ£©¡£³óª£¬µ«ÊÇÓÐЧ¡£ÀýÈ磺

>>> 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!' ÊÇÔª×é·â×°£¨sequence packing£©µÄÒ»¸öÀý×Ó£ºÖµ 12345£¬ 54321 ºÍ 'hello!' ±»·â×°½øÔª×é¡£ÆäÄæ²Ù×÷¿ÉÄÜÊÇÕâÑù£º

>>> x, y, z = t

This is called, appropriately enough, sequence unpacking. Sequence unpacking requires that the list of variables on the left have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking!

Õâ¸öµ÷Óñ»³ÆΪÐòÁвð·â·Ç³£ºÏÊÊ¡£ÐòÁвð·âÒªÇó×ó²àµÄ±äÁ¿ÊýÄ¿ÓëÐòÁеÄÔªËظöÊýÏàͬ¡£Òª×¢ÒâµÄÊǿɱä²ÎÊý£¨multiple assignment £©ÆäʵֻÊÇÔª×é·â×°ºÍÐòÁвð·âµÄÒ»¸ö½áºÏ£¡

There is a small bit of asymmetry here: packing multiple values always creates a tuple, and unpacking works for any sequence.

ÕâÀïÓÐÒ»µã²»¶Ô³Æ£º·â×°¶àÖزÎÊýͨ³£»á´´½¨Ò»¸öÔª×飬¶ø²ð·â²Ù×÷¿ÉÒÔ×÷ÓÃÓÚÈκÎÐòÁС£


5.4 Dictionaries ×Öµä

Another useful data type built into Python is the dictionary. 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 their append() and extend() methods, as well as slice and indexed assignments.

ÁíÒ»¸ö·Ç³£ÓÐÓõÄPythonÄÚ½¨Êý¾ÝÀàÐÍÊÇ×ֵ䡣×ÖµäÔÚijЩÓïÑÔÖпÉÄܳÆΪ¡°ÁªºÏÄڴ桱£¨``associative memories''£©»ò¡°ÁªºÏÊý×顱£¨``associative arrays''£©¡£ÐòÁÐÊÇÒÔÁ¬ÐøµÄÕûÊýΪË÷Òý£¬Óë´Ë²»Í¬µÄÊÇ£¬×ÖµäÒԹؼü×ÖΪË÷Òý£¬¹Ø¼ü×Ö¿ÉÒÔÊÇÈÎÒâ²»¿É±äÀàÐÍ£¬Í¨³£ÓÃ×Ö·û´®»òÊýÖµ¡£Èç¹ûÔª×éÖÐÖ»°üº¬×Ö·û´®ºÍÊý×Ö£¬Ëü¿ÉÒÔ×öΪ¹Ø¼ü×Ö£¬Èç¹ûËüÖ±½Ó»ò¼ä½ÓµÄ°üº¬Á˿ɱä¶ÔÏ󣬾Ͳ»Äܵ±×ö¹Ø¼ü×Ö¡£²»ÄÜÓÃÁ´±í×ö¹Ø¼ü×Ö£¬ÒòΪÁ´±í¿ÉÒÔÓÃËüÃǵÄappend() ºÍ extend()·½·¨£¬»òÕßÓÃÇÐƬ¡¢»òÕßͨ¹ý¼ìË÷±äÁ¿À´¼´Ê±¸Ä±ä¡£

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.

Àí½â×ÖµäµÄ×î¼Ñ·½Ê½ÊÇ°ÑËü¿´×öÎÞÐòµÄ¹Ø¼ü×Ö£ºÖµ ¶Ô£¨key:value pairs£©¼¯ºÏ£¬¹Ø¼ü×Ö±ØÐëÊÇ»¥²»ÏàͬµÄ£¨ÔÚͬһ¸ö×ÖµäÖ®ÄÚ£©¡£Ò»¶Ô´óÀ¨ºÅ´´½¨Ò»¸ö¿ÕµÄ×ֵ䣺{}¡£³õʼ»¯Á´±íʱ£¬ÔÚ´óÀ¨ºÅÄÚ·ÅÖÃÒ»×鶺ºÅ·Ö¸ôµÄ¹Ø¼ü×Ö£ºÖµ¶Ô£¬ÕâÒ²ÊÇ×ÖµäÊä³öµÄ·½Ê½¡£

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À´É¾³ý¹Ø¼ü×Ö£ºÖµ¶Ô¡£Èç¹ûÄãÓÃÒ»¸öÒѾ­´æÔڵĹؼü×Ö´æ´¢Öµ£¬ÒÔǰΪ¸Ã¹Ø¼ü×Ö·ÖÅäµÄÖµ¾Í»á±»ÒÅÍü¡£ÊÔͼÎöÈ¡´ÓÒ»¸ö²»´æÔڵĹؼü×ÖÖжÁÈ¡Öµ»áµ¼Ö´íÎó¡£

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in random order (if you want it sorted, just apply the sort() method to the list of keys). To check whether a single key is in the dictionary, use the has_key() method of the dictionary.

×ÖµäµÄ keys()·½·¨·µ»ØÓÉËùÓйؼü×Ö×é³ÉµÄÁ´±í£¬¸ÃÁ´±íµÄ˳Ðò²»¶¨£¨Èç¹ûÄãÐèÒªËüÓÐÐò£¬Ö»Äܵ÷Óùؼü×ÖÁ´±íµÄsort() ·½·¨£©¡£Ê¹ÓÃ×ÖµäµÄ has_key()·½·¨¿ÉÒÔ¼ì²é×ÖµäÖÐÊÇ·ñ´æÔÚijһ¹Ø¼ü×Ö¡£

Here is a small example using a dictionary:

ÕâÊÇÒ»¸ö¹ØÓÚ×ÖµäÓ¦ÓõÄСʾÀý£º

>>> 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}
>>> tel.keys()
['guido', 'irv', 'jack']
>>> tel.has_key('guido')
True

The dict() constructor builds dictionaries directly from lists of key-value pairs stored as tuples. When the pairs form a pattern, list comprehensions can compactly specify the key-value list.

Á´±íÖд洢¹Ø¼ü×Ö-Öµ¶ÔÔª×éµÄ»°£¬×Öµä¿ÉÒÔ´ÓÖÐÖ±½Ó¹¹Ôì¡£¹Ø¼ü×Ö-Öµ¶ÔÀ´×ÔÒ»¸öģʽʱ£¬¿ÉÒÔÓÃÁ´±íÍƵ¼Ê½¼òµ¥µÄ±í´ï¹Ø¼ü×Ö-ÖµÁ´±í¡£

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> dict([(x, x**2) for x in vec])     # use a list comprehension
{2: 4, 4: 16, 6: 36}


5.5 Ñ­»·¼¼ÇÉ Looping Techniques

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

ÔÚ×ÖµäÖÐÑ­»·Ê±£¬¹Ø¼ü×ֺͶÔÓ¦µÄÖµ¿ÉÒÔʹÓà iteritems()·½·¨Í¬Ê±½â¶Á³öÀ´¡£

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
...     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 %s?  It is %s.' % (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.


5.6 ÉîÈëÌõ¼þ¿ØÖÆ More on Conditions

The conditions used in while and if statements above can contain other operators besides 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 ºÍ±È½ÏÁ½¸ö¶ÔÏóÊÇ·ñÏàͬ£»ÕâÖ»ºÍÖîÈçÁ´±íÕâÑùµÄ¿É±ä¶ÔÏóÓйء£ËùÓеıȽϲÙ×÷·û¾ßÓÐÏàͬµÄÓÅÏȼ¶£¬µÍÓÚËùÓеÄÊýÖµ²Ù×÷¡£

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 by the Boolean operators and and or, and the outcome of a comparison (or of any other Boolean expression) may be negated with not. These all have lower priorities than comparison operators again; 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. Of course, 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. In general, the return value of a short-circuit operator, when used as a general value and not as a Boolean, is the last evaluated argument.

Âß¼­²Ù×÷·û and ºÍ or Ò²³Æ×÷¶Ì·²Ù×÷·û£ºËüÃǵIJÎÊý´Ó×óÏòÓÒ½âÎö£¬Ò»µ©½á¹û¿ÉÒÔÈ·¶¨¾ÍÍ£Ö¹¡£ÀýÈ磬Èç¹û 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.

ÐèҪעÒâµÄÊÇPythonÓëC²»Í¬£¬ÔÚ±í´ïʽÄÚ²¿²»Äܸ³Öµ¡£C ³ÌÐòÔ±¾­³£¶Ô´Ë±§Ô¹£¬²»¹ýËü±ÜÃâÁËÒ»ÀàÔÚ C ³ÌÐòÖÐ˾¿Õ¼û¹ßµÄ´íÎó£ºÏëÒªÔÚ½âÎöʽÖÐʹ == ʱÎóÓÃÁË = ²Ù×÷·û¡£


5.7 ±È½ÏÐòÁкÍÆäËüÀàÐÍ 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 ASCII ordering for individual characters. Some examples of comparisons between sequences with the same types:

ÐòÁжÔÏó¿ÉÒÔÓëÏàͬÀàÐ͵ÄÆäËü¶ÔÏó±È½Ï¡£±È½Ï²Ù×÷°´ ×ÖµäÐò ½øÐУºÊ×ÏȱȽÏÇ°Á½¸öÔªËØ£¬Èç¹û²»Í¬£¬¾Í¾ö¶¨Á˱ȽϵĽá¹û£»Èç¹ûÏàͬ£¬¾Í±È½ÏºóÁ½¸öÔªËØ£¬ÒÀ´ËÀàÍÆ£¬Ö±µ½ËùÓÐÐòÁж¼Íê³É±È½Ï¡£Èç¹ûÁ½¸öÔªËر¾Éí¾ÍÊÇͬÑùÀàÐ͵ÄÐòÁУ¬¾ÍµÝ¹é×ÖµäÐò±È½Ï¡£Èç¹ûÁ½¸öÐòÁеÄËùÓÐ×ÓÏÏàµÈ£¬¾ÍÈÏΪÐòÁÐÏàµÈ¡£Èç¹ûÒ»¸öÐòÁÐÊÇÁíÒ»¸öÐòÁеijõʼ×ÓÐòÁУ¬½Ï¶ÌµÄÒ»¸öÐòÁоÍСÓÚÁíÒ»¸ö¡£×Ö·û´®µÄ×ÖµäÐò°´ÕÕµ¥×Ö·ûµÄ ASCII ˳Ðò¡£ÏÂÃæÊÇͬÀàÐÍÐòÁÐÖ®¼ä±È½ÏµÄһЩÀý×Ó£º

(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 is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.5.1

ÐèҪעÒâµÄÊDz»Í¬ÀàÐ͵ĶÔÏó±È½ÏÊǺϷ¨µÄ¡£Êä³ö½á¹ûÊÇÈ·¶¨¶ø·ÇÈÎÒâµÄ£ºÀàÐÍ°´ËüÃǵÄÃû×ÖÅÅÐò¡£Òò¶ø£¬Ò»¸öÁ´±í£¨list£©×ÜÊÇСÓÚÒ»¸ö×Ö·û´®£¨string£©£¬Ò»¸ö×Ö·û´®£¨string£©×ÜÊÇСÓÚÒ»¸öÔª×飨tuple£©µÈµÈ¡£ÊýÖµÀàÐͱȽÏʱ»áͳһËüÃǵÄÊý¾ÝÀàÐÍ£¬ËùÒÔ0µÈÓÚ0.0£¬µÈµÈ¡£5.2



Footnotes

... etc.5.1
The rules for comparing objects of different types should not be relied upon; they may change in a future version of the language.
...ÐèҪעÒâµÄÊDz»Í¬ÀàÐ͵ĶÔÏó±È½ÏÊǺϷ¨µÄ¡£Êä³ö½á¹ûÊÇÈ·¶¨¶ø·ÇÈÎÒâµÄ£ºÀàÐÍ°´ËüÃǵÄÃû×ÖÅÅÐò¡£Òò¶ø£¬Ò»¸öÁ´±í£¨list£©×ÜÊÇСÓÚÒ»¸ö×Ö·û´®£¨string£©£¬Ò»¸ö×Ö·û´®£¨string£©×ÜÊÇСÓÚÒ»¸öÔª×飨tuple£©µÈµÈ¡£ÊýÖµÀàÐͱȽÏʱ»áͳһËüÃǵÄÊý¾ÝÀàÐÍ£¬ËùÒÔ0µÈÓÚ0.0£¬µÈµÈ¡£5.2
²»Í¬ÀàÐͶÔÏóµÄ±È½Ï¹æÔò²»ÒÀÀµÓÚ´Ë£¬ËüÃÇÓпÉÄÜ»áÔÚPythonÓïÑԵĺó¼Ì°æ±¾Öиı䡣
ÒëÕߣºÁõöÎ(march.liu AT gmail DOT com) ÓÉ£ºlimodouת(limodou AT gmail DOT com)
See About this document... for information on suggesting changes.