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:html
del语句:删除列表中指定位置的元素。与列表的pop()方法有点不一样的是:del语句能够删除整个列表或者是移除子列表。例如:python
>>> 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还能够删除整个列表变量:express
>>> 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另外的做用。app
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.less
咱们已经看到列表和字符串有许多共同的属性,例如索引和切片操做。他们只是序列类型的两个例子(参考 序列类型--列表,元组,range)。由于Python正不断的处于更新中,其余序列类型可能会再被加进来。元组:另外一个标准的序列数据类型。编程语言
A tuple consists of a number of values separated by commas, for instance:ide
元组是由一些由逗号分隔的元素组成,例如:函数
>>> 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!' 是一个元组打包的例子:元素12345, 54321 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.
这个序列的解包调用例子,恰好合适。其余序列均可以放到等号右边做解包操做。序列的解包操做要求等号左边的变量和序列右边的元素个数要相同。注意,多重赋值仅仅只是元组的一个打包和解包的一个结合。
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'}
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}
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']
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语言中常见的一个错误:原本想书写==却写了=。
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异常。