Chapter 5 Python Data Structure
This chapter discusses the use of some of the data types that have been discussed in more detail and introduces some new types.
5.1 List
There are other methods for list data types. Here are all the methods of list objects:
insert(i, x) --- Insert an item at the specified position. The first independent variable is which element to be inserted before, and is represented by a subscript. For example, (0, x) is inserted in front of the list, (len(a), x) is equivalent to (x) .
append(x) --- equivalent to (len(a), x)
index(x) --- Search the value x in the list and return the subscript of the first element with a value x. An error occurred when not found.
remove(x) --- Delete the first element with x value from the list, an error occurred when it cannot be found.
sort() ----Sort list elements in place. Note that this method changes the list instead of returning the sorted list.
reverse() ----- Inverse the list elements. Change the list.
count(x) --- Returns the number of times x appears in the list.
The following example uses all list methods:
>>> a = [66.6, 333, 333, 1, 1234.5]
>>> print (333), (66.6), ('x')
2 1 0
>>> (2, -1)
>>> (333)
>>> a
[66.6, 333, -1, 333, 1, 1234.5, 333]
>>> (333)
1
>>> (333)
>>> a
[66.6, -1, 333, 1, 1234.5, 333]
>>> ()
>>> a
[333, 1234.5, 1, 333, -1, 66.6]
>>> ()
>>> a
[-1, 1, 66.6, 333, 333, 1234.5]
5.1.1 Function programming tools
There are some functional programming style things in Python, such as the lambda form we saw earlier. There are three very useful built-in functions about lists: filter(), map() and reduce().
"filter(function, sequence)" returns a sequence (as the same type as the original one as possible), the sequence elements are those filtered out by the specified functions in the original sequence, and the filtering rule is "function (sequence element)=true". filter() can be used to fetch a subset that meets the conditions. For example, to calculate some prime numbers:
>>> 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 the specified function for each item in the specified sequence, and the result is a list of return values. map() can implicitly loop the sequence. For example, to calculate the cubic power, you can:
>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
There can be multiple sequences as independent variables. At this time, the specified function must also have the same number of independent variables. The function takes the corresponding element from each sequence as independent variables and calls it (if a certain sequence is shorter than others, the value taken is None). If the specified function is None, map() treats it as a constant-same function that returns its own independent variable. When a function uses None, specify multiple sequences, you can match multiple sequences. For example, "map(None, list1, list2)" can combine two lists into a list of paired values. See the following example:
>>> seq = range(8)
>>> def square(x): return x*x
...
>>> map(None, seq, map(square, seq))
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]
"reduce(function, sequence)" is used to perform operations like accumulation. The function here is a function with two sub-variables. reduce() first calls the function to get a result on the first two items of the sequence, and then calls the function to the result and the next item of the sequence to get a new result. This way to the end of the sequence. For example, to calculate the sum of 1 to 10:
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
If there is only one value in the sequence, this value will be returned, and an exception will be generated when the sequence is empty. A third argument can be specified as the initial value. When there is an initial value, the function will return the initial value. Otherwise, the function will first act on the initial value and the first term of the sequence, and then act on the result and the next term of the sequence, so as to the end of the sequence. For example:
>>> def sum(seq):
... def add(x,y): return x+y
... return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0
5.2 del statement
As we see above, the remove() method of the list can delete a certain value item from the list, and we can also use the del statement to delete the item with the specified subscript. You can also use the del statement to delete a fragment from the list (we used to delete the fragment by assigning an empty list to the fragment). For example:
>>> 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, for example:
>>> del a
An error occurs when a variable is deleted and then referenced (unless it is assigned a value again). We will see some other applications of del later.
5.3 Sequence tables and sequences
We see that lists and strings have many things in common, such as subscripts and fragment operations. They all belong to the sequence data type. Because Python is a constantly evolving language, other sequence data types may be added in the future. There is now a standard sequence data type called a tuple.
The sequence table is separated by a series of values by commas, for example:
>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Sequence tables allow nesting:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
The output sequence table is always surrounded by brackets, which ensures that the nested sequence table is interpreted correctly. There can be brackets or no brackets when input, and often there must be brackets (if the sequence table is part of a large expression).
Sequence tables have many uses, such as (x, y) coordinate pairs, employee records in databases, and so on. Sequence tables are as immutable as strings: assignments to certain items of the sequence table are not allowed.
When generating a sequence table, there are special provisions for the sequence table of 0 or 1: the empty sequence table is represented by a pair of empty brackets; the sequence table of only one is represented by one followed by a jitter (meaning that it is not enough to put this value in brackets). This writing is not beautiful enough, but it is very effective. For example:
>>> empty = ()
>>> singleton = 'hello', # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)
The statement t = 12345, 54321, 'hello!' is an example of sequence table packaging: 12345, 54321 and 'hello!' are packaged into a sequence table. The opposite operation is also allowed, for example:
>>> x, y, z = t
This is called unpacking of the sequence table. The unpacking of the sequence table requires that the number of variables on the left side of the equal sign is equal to the length of the sequence table. Note that multiple assignments are just a combination of sequence table packaging and sequence table unpacking. Sometimes a similar operation is performed on a list, that is, the list is unpacked. Just write each variable into a list and unpack it:
>>> a = ['spam', 'eggs', 100, 1234]
>>> [a1, a2, a3, a4] = a
5.4 Dictionary
Another useful data type built into Python is the dictionary. Dictionaries are sometimes called "association memory" or "association array" in other languages. A dictionary is not like a sequence. It is not indexed with a number subscript within a range, but with key values, which can be of any immutable type. Strings and values can be used as key values. If the sequence table contains only strings, values, or sequence tables, the sequence table can also be used as key values. Lists cannot be used as key values, because lists can be changed in-place using their append() method.
It is best to regard the dictionary as a collection of unsorted "key value: value", where key values are different from each other in the same dictionary. A pair of empty braces produces an empty dictionary: {}. Adding a "key value:value" pair separated by a comma in the braces can add the initial key value and value pairs into the dictionary, and the dictionary is also displayed in the output. The main operation on a dictionary is to save a value with a certain key value, and to find the corresponding value after giving the key value. You can also use del to delete a key value: value pair. If you save a value with an already defined key value, the original vegetation will be forgotten. It will make an error if you use non-existent key values to search.
The keys() method of the dictionary object returns a list of all key values in the dictionary, in a random order. When sorting is required, just use the sort() method for the returned key value list. To check whether a key value is in the dictionary, use the has_key() method of the dictionary.
Here is a simple example of dictionary use:
>>> 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}
>>> ()
['guido', 'irv', 'jack']
>>> tel.has_key('guido')
1
5.5 Further discussion of conditions
The conditions used in while statements and if statements can also include other operators in addition to using comparisons. Comparison operators "in" and "not in" can check whether a value is in a sequence. The operators "is" and "is not " compare whether the two objects happen to be the same object. This mutable object such as the object list makes sense. All comparison operations have the same priority, and the comparison operations have lower priority than all numerical operations.
Comparison allows for continuation, for example, a < b == c checks whether a is less than or equal to b and b is equal to c.
Comparison can be connected by logical operators and or, and the result of comparison (or any other logical expression) can be inversely used by not. The logical operator is lower than all comparison operators. Among the logical operators, not has the highest priority and or has the lowest priority, so "A and not B or C" should be interpreted as "(A and (not B)) or C". Of course, the required combination conditions can be represented by brackets.
Logical operators and and or are called "short-circuit" operators: the expressions on both sides of the operator are first calculated on the left. If the result on the left is known, the overall result is known, the expression on the right is no longer calculated. For example, if A and C are true and B are false, "A and B and C" will not calculate expression C. Generally, when the operation result of the short-circuit operator is not used as a logical value, the value of the last evaluated expression is returned.
The results of comparison or other logical expressions can be assigned to a variable. For example:
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
Note that Python and C are different, and assignment cannot be performed in expressions.
5.6 Comparison of sequences with other types
Sequence objects can be compared with other objects of the same sequence type. Use dictionary order for comparison: first compare the first two terms, if these two terms are different, the result can be determined; if these two terms are the same, compare the following two terms and continue until there is a sequence that ends. If the two terms are also sequences of the same type, then recursive dictionary order comparison is performed. If all the terms of the two sequences are equal, the two sequences are equal. If one sequence is an initial subsequence of another sequence, the shortest one is the smaller one. The dictionary order comparison of strings is performed in the ASCII order of each character. Here are some examples of sequence comparison:
(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 comparisons of different types of objects are currently legal. The result is certain but meaningless: the different types are sorted by the name of the type. So, a list is always smaller than a string, a string is always smaller than a tuple, etc. However, such comparison rules cannot be relied on in the program, and the language implementation may change. Different numerical types can be compared by numerical values, so 0 equals 0.0, etc.
Previous page12345678910Next pageRead the full text