You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Phần này sẽ mô tả những gì bạn đã học qua và đi sâu hơn, bổ sung một số phần mới nữa nhé!
5
+
# 5. Data Structures
4
6
5
-
### 5.1. Nhiều hơn về Lists
7
+
Phần này sẽ mô tả những gì bạn đã học qua và đi sâu hơn, bổ sung một số phần mới nữa nhé!
8
+
9
+
## 5.1. Nhiều hơn về Lists
6
10
7
11
Kiểu dữ liệu list có khá nhiều phương thức để xử lý. Sau đây là tất cả các cách ấy:
8
12
9
-
`list.append`\(_x_\)Nối thêm một phần tử vào cuối danh sách. Tương tự như là `a[len(a):] = [x]`.`list.extend`\(_iterable_\)
13
+
`list.append`\(_x_\) Nối thêm một phần tử vào cuối danh sách. Tương tự như là `a[len(a):] = [x]`.`list.extend`\(_iterable_\)
10
14
11
15
mở rộng danh sách bằng cách thêm tất cả các phần tử từ iterable. Tương tự `a[len(a):] = iterable`.`list.insert`\(_i_, _x_\)
12
16
@@ -57,7 +61,7 @@ An example that uses most of the list methods:>>>
57
61
58
62
You might have noticed that methods like `insert`, `remove` or `sort` that only modify the list have no return value printed – they return the default `None`. [\[1\]](https://docs.python.org/3/tutorial/datastructures.html#id3) This is a design principle for all mutable data structures in Python.
59
63
60
-
####5.1.1. Using Lists as Stacks
64
+
### 5.1.1. Using Lists as Stacks
61
65
62
66
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:>>>
63
67
@@ -79,7 +83,7 @@ The list methods make it very easy to use a list as a stack, where the last elem
79
83
[3, 4]
80
84
```
81
85
82
-
####5.1.2. Using Lists as Queues
86
+
### 5.1.2. Using Lists as Queues
83
87
84
88
It is also possible to use a list as a queue, where the first element added is the first element retrieved \(“first-in, first-out”\); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow \(because all of the other elements have to be shifted by one\).
85
89
@@ -98,7 +102,7 @@ To implement a queue, use [`collections.deque`](https://docs.python.org/3/librar
98
102
deque(['Michael', 'Terry', 'Graham'])
99
103
```
100
104
101
-
####5.1.3. List Comprehensions
105
+
### 5.1.3. List Comprehensions
102
106
103
107
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
104
108
@@ -189,7 +193,7 @@ List comprehensions can contain complex expressions and nested functions:>>
189
193
['3.1', '3.14', '3.142', '3.1416', '3.14159']
190
194
```
191
195
192
-
####5.1.4. Nested List Comprehensions
196
+
### 5.1.4. Nested List Comprehensions
193
197
194
198
The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.
195
199
@@ -245,7 +249,7 @@ In the real world, you should prefer built-in functions to complex flow statemen
245
249
246
250
See [Unpacking Argument Lists](https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments) for details on the asterisk in this line.
247
251
248
-
###5.2. The [`del`](https://docs.python.org/3/reference/simple_stmts.html#del) statement
252
+
## 5.2. The [`del`](https://docs.python.org/3/reference/simple_stmts.html#del) statement
249
253
250
254
There is a way to remove an item from a list given its index instead of its value: the [`del`](https://docs.python.org/3/reference/simple_stmts.html#del) statement. This differs from the `pop()` method which returns a value. The [`del`](https://docs.python.org/3/reference/simple_stmts.html#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:>>>
251
255
@@ -270,7 +274,7 @@ There is a way to remove an item from a list given its index instead of its valu
270
274
271
275
Referencing the name `a` hereafter is an error \(at least until another value is assigned to it\). We’ll find other uses for [`del`](https://docs.python.org/3/reference/simple_stmts.html#del) later.
272
276
273
-
###5.3. Tuples and Sequences
277
+
## 5.3. Tuples and Sequences
274
278
275
279
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](https://docs.python.org/3/library/stdtypes.html#typesseq)\). Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the_tuple_.
276
280
@@ -322,7 +326,7 @@ The statement `t = 12345, 54321, 'hello!'` is an example of _tuple packing_: the
322
326
323
327
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.
324
328
325
-
###5.4. Sets
329
+
## 5.4. Sets
326
330
327
331
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.
328
332
@@ -363,7 +367,7 @@ Similarly to [list comprehensions](https://docs.python.org/3/tutorial/datastruct
363
367
{'r', 'd'}
364
368
```
365
369
366
-
###5.5. Dictionaries
370
+
## 5.5. Dictionaries
367
371
368
372
Another useful data type built into Python is the _dictionary_\(see [Mapping Types — dict](https://docs.python.org/3/library/stdtypes.html#typesmapping)\). 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()`.
369
373
@@ -417,7 +421,7 @@ When the keys are simple strings, it is sometimes easier to specify pairs using
417
421
{'sape': 4139, 'jack': 4098, 'guido': 4127}
418
422
```
419
423
420
-
###5.6. Looping Techniques
424
+
## 5.6. Looping Techniques
421
425
422
426
When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the `items()` method.>>>
423
427
@@ -494,7 +498,7 @@ It is sometimes tempting to change a list while you are looping over it; however
494
498
[56.2, 51.7, 55.3, 52.5, 47.8]
495
499
```
496
500
497
-
###5.7. More on Conditions
501
+
## 5.7. More on Conditions
498
502
499
503
The conditions used in `while` and `if` statements can contain any operators, not just comparisons.
500
504
@@ -517,7 +521,7 @@ It is possible to assign the result of a comparison or other Boolean expression
517
521
518
522
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.
519
523
520
-
###5.8. Comparing Sequences and Other Types
524
+
## 5.8. Comparing Sequences and Other Types
521
525
522
526
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:
If you quit from the Python interpreter and enter it again, the definitions you have made \(functions and variables\) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a _script_. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program.
There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities.
0 commit comments