Đôi khi, có thể cần phải có phạm vi giữa một số nằm trong danh sách, cho các ứng dụng đó chúng tôi yêu cầu để có được yếu tố đầu tiên và cuối cùng của danh sách. Hãy để thảo luận về các cách nhất định để có được yếu tố đầu tiên và cuối cùng của danh sách. & NBSP;
Phương pháp số 1: Sử dụng chỉ mục danh sách
Sử dụng các chỉ số danh sách bên trong danh sách chính có thể thực hiện nhiệm vụ cụ thể này. Đây là phương pháp ngây thơ nhất để đạt được nhiệm vụ cụ thể này mà người ta có thể nghĩ đến. & NBSP;
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1__16
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 40
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 43
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 45
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 46
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 48
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]4
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 2: Sử dụng Danh sách cắt
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1__16
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 40
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 43
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 45
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 46
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 48
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]4
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 2: Sử dụng Danh sách cắt
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
Python3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]2
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1__16
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]7
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 40
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 41
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]3
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 43
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 44
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 45
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 46
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 48
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]5
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]6
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]1
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]8
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]9
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]4
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Phương pháp số 2: Sử dụng Danh sách cắtmethod
Người ta cũng có thể sử dụng kỹ thuật cắt danh sách để thực hiện nhiệm vụ cụ thể là có được yếu tố đầu tiên và cuối cùng. Chúng ta có thể sử dụng bước của toàn bộ danh sách để bỏ qua phần tử cuối cùng sau phần tử đầu tiên. & NBSP;
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : 1 4
1The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
3 list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
6list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
7list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
8The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : 1 4
6The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
5The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
4
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]15
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]16
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]17
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]18
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]19
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]20
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]21
Phương pháp số 3: Sử dụng danh sách hiểu
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]25
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]16
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]27
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]28
Danh sách hiểu có thể được sử dụng để cung cấp tốc ký cho kỹ thuật vòng lặp để tìm các yếu tố đầu tiên và cuối cùng của danh sách. Phương pháp tìm kiếm ngây thơ được chuyển đổi thành một dòng bằng phương pháp này. & NBSP;
Output:
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : 1 4
Trả về chỉ số thấp nhất trong danh sách OBJ xuất hiệnsequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.
list.insert [index, obj]
Chèn đối tượng obj vào danh sách tại chỉ mục bù
Xóa và trả về đối tượng cuối cùng hoặc OBJ khỏi danh sách
Xóa đối tượng OBJ khỏi danh sách
Đảo ngược các đối tượng của danh sách tại chỗ
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
Tương tự như các chỉ số chuỗi, chỉ số danh sách bắt đầu từ 0 và danh sách có thể được cắt lát, được nối, v.v.
Truy cập các giá trị trong danh sách
Để truy cập các giá trị trong danh sách, hãy sử dụng dấu ngoặc vuông để cắt cùng với chỉ mục hoặc chỉ số để có được giá trị có sẵn tại chỉ mục đó. Ví dụ -
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]
Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
Cập nhật danh sách
Bạn có thể cập nhật một hoặc nhiều yếu tố của danh sách bằng cách đưa lát ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách với phương thức append []. Ví dụ -
#!/usr/bin/python list = ['physics', 'chemistry', 1997, 2000]; print "Value available at index 2 : " print list[2] list[2] = 2001; print "New value available at index 2 : " print list[2]
Lưu ý - phương thức append [] được thảo luận trong phần tiếp theo. − append[] method is discussed in subsequent section.
Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -
Value available at index 2 : 1997 New value available at index 2 : 2001
Cập nhật danh sách
Bạn có thể cập nhật một hoặc nhiều yếu tố của danh sách bằng cách đưa lát ở phía bên trái của toán tử gán và bạn có thể thêm vào các phần tử trong danh sách với phương thức append []. Ví dụ -
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000]; print list1 del list1[2]; print "After deleting value at index 2 : " print list1
Lưu ý - phương thức append [] được thảo luận trong phần tiếp theo.
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]0
Xóa các yếu tố danh sách − remove[] method is discussed in subsequent section.
Để xóa phần tử danh sách, bạn có thể sử dụng câu lệnh DEL nếu bạn biết chính xác [các] phần tử nào bạn đang xóa hoặc phương thức xóa [] nếu bạn không biết. Ví dụ -
Khi mã trên được thực thi, nó sẽ tạo ra kết quả sau -
Lưu ý - Remove [] Phương thức được thảo luận trong phần tiếp theo.
Biểu hiện Python | 3 | Kết quả |
Sự mô tả | Len [[1, 2, 3]] | Chiều dài |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | Kết nối |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!']] | Sự lặp lại |
3 trong [1, 2, 3] | ĐÚNG VẬY | Tư cách thành viên |
cho x trong [1, 2, 3]: in x,
1 2 3
Lặp đi lặp lại
Lập chỉ mục, cắt và ma trậnBiểu hiện Python | Kết quả | Sự mô tả |
Len [[1, 2, 3]] | Chiều dài | [1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] | Kết nối | ['Hi!'] * 4 |
['Hi!', 'Hi!', 'Hi!', 'Hi!']]
Sự lặp lại
1 | ĐÚNG VẬY Tư cách thành viên |
2 | len[list] cho x trong [1, 2, 3]: in x, |
3 | max[list] 1 2 3 |
4 | min[list] Lặp đi lặp lại |
5 | list[seq] Lập chỉ mục, cắt và ma trận |
Bởi vì danh sách là các chuỗi, lập chỉ mục và cắt làm việc giống như cách cho các danh sách như chúng làm cho các chuỗi.
1 | list.append[obj]The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]1 |
2 | list.count[obj] L [2] |
3 | list.extend[seq] THƯ RÁC! |
4 | list.index[obj] Độ lệch bắt đầu từ 0 |
5 | L [-2] Thư rác |
6 | list.pop[obj=list[-1]] Tiêu cực: Đếm từ bên phải |
7 | list.remove[obj] L [1:] |
8 | list.reverse[] ['Spam', 'Spam!']] |
9 | list.sort[[func]] Cắt lát phần |