Lấy mục trong danh sách python

Đôi khi, có thể cần phải lấy phạm vi giữa một số nằm trong danh sách, đối với những ứng dụng như vậy, chúng tôi yêu cầu lấy phần tử đầu tiên và cuối cùng của danh sách. Hãy thảo luận về một số cách nhất định để lấy phần tử đầu tiên và cuối cùng của danh sách.  

Phương pháp số 1. Sử dụng chỉ mục danh sách

Sử dụng các chỉ mục danh sách bên trong danh sách chính có thể thực hiện tác 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ĩ ra.  

Python3




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]
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, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
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]
2
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]
2
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]
2
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]
0

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1

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]
5

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
80
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]
82
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
83
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
84
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
87

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
88

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]
41
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]
44

đầu ra.

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp #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à lấy phần 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 để chuyển đến phần tử cuối cùng sau phần tử đầu tiên.  

Python3




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]
47

 

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, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
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]
2
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]
2
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]
2
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]
0

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1

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]
47

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]
80
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]
73_______974____975
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
0

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
88

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]
41
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]
44

đầu ra.

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp #3. Sử dụng hiểu danh sách

Khả năng hiểu danh sách có thể được sử dụng để cung cấp cách viết tắt cho kỹ thuật vòng lặp để tìm phần 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.  

Python3




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]
88

 

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, 4]
0
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
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]
2
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]
2
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]
2
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]
0

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1

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]
88

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]
80
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]
14
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]
3
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
83
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]
85
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
1
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
23

 

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
88

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]
41
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]
44

đầu ra.

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]

Phương pháp #4. Sử dụng bản đồ và phương thức __getitem__

Với các phương thức này, về cơ bản, chúng tôi đang truy cập phần tử tại một vị trí nhất định từ danh sách. Chúng tôi khớp một danh sách chứa một phần tử và một danh sách chứa chỉ mục của phần tử mà chúng tôi muốn và ánh xạ hai mảng và sử dụng __getitem__ để lấy phần tử từ chỉ mục được lưu trữ trong danh sách chỉ mục

Chủ Đề