Hướng dẫn how do you find the last and first index in python? - làm thế nào để bạn tìm thấy chỉ mục cuối cùng và đầu tiên trong python?

Đô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

test_list = [

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

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

Các

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
6
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
9

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

test_list = [

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

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

Các

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
6
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
9

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

test_list = [

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

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

Các

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
6
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
9

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;

Các

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

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

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

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

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

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

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

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

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

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

Có lẽ hai cách hiệu quả nhất để tìm chỉ số cuối cùng:

def rindex(lst, value):
    lst.reverse()
    i = lst.index(value)
    lst.reverse()
    return len(lst) - i - 1
def rindex(lst, value):
    return len(lst) - operator.indexOf(reversed(lst), value) - 1

Cả hai chỉ dành thêm không gian O (1) và hai đảo ngược tại chỗ của giải pháp đầu tiên nhanh hơn nhiều so với việc tạo một bản sao ngược. Hãy so sánh nó với các giải pháp khác được đăng trước đó:

def rindex(lst, value):
    return len(lst) - lst[::-1].index(value) - 1

def rindex(lst, value):
    return len(lst) - next(i for i, val in enumerate(reversed(lst)) if val == value) - 1

Kết quả điểm chuẩn, các giải pháp của tôi là các giải pháp màu đỏ và màu xanh lá cây:

Hướng dẫn how do you find the last and first index in python? - làm thế nào để bạn tìm thấy chỉ mục cuối cùng và đầu tiên trong python?

Đây là để tìm kiếm một số trong danh sách một triệu số. Trục X dành cho vị trí của phần tử được tìm kiếm: 0% có nghĩa là nó ở đầu danh sách, 100% có nghĩa là nó ở cuối danh sách. Tất cả các giải pháp là nhanh nhất tại vị trí 100%, với hai giải pháp

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
27 mất khá nhiều thời gian cho điều đó, giải pháp đảo ngược đôi thời gian và bản sao đảo ngược mất nhiều thời gian.

Nhìn kỹ hơn vào đầu bên phải:

Hướng dẫn how do you find the last and first index in python? - làm thế nào để bạn tìm thấy chỉ mục cuối cùng và đầu tiên trong python?

Tại vị trí 100%, giải pháp sao chép ngược và giải pháp ngược lại dành toàn bộ thời gian cho các đảo ngược (

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
28 là tức thì), vì vậy chúng tôi thấy rằng hai đảo ngược tại chỗ nhanh gấp khoảng bảy lần so với việc tạo bản sao ngược .

Trên đây là với

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
29, điều này tạo ra nhiều đối tượng INT tuần tự trong bộ nhớ, cực kỳ thân thiện với bộ đệm. Hãy làm lại sau khi xáo trộn danh sách với
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
30 (có thể ít thực tế hơn, nhưng thú vị):

Hướng dẫn how do you find the last and first index in python? - làm thế nào để bạn tìm thấy chỉ mục cuối cùng và đầu tiên trong python?

Hướng dẫn how do you find the last and first index in python? - làm thế nào để bạn tìm thấy chỉ mục cuối cùng và đầu tiên trong python?

Tất cả đều chậm hơn rất nhiều, như mong đợi. Giải pháp sao chép ngược phải chịu nhiều nhất, ở mức 100% hiện mất khoảng 32 lần (!) Miễn là dung dịch đảo ngược kép. Và giải pháp ____ 131 hiện chỉ là nhanh thứ hai sau vị trí 98%.

Nhìn chung, tôi thích giải pháp

The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
32 nhất, vì đây là giải pháp nhanh nhất trong nửa hoặc một phần tư của tất cả các địa điểm, có lẽ là địa điểm thú vị hơn nếu bạn thực sự làm
The original list is : [1, 5, 6, 7, 4]
The first and last element of list are : [1, 4]
33 cho một cái gì đó. Và nó chỉ chậm hơn một chút so với giải pháp đảo ngược kép ở các vị trí trước đó.

Tất cả các điểm chuẩn được thực hiện với CPython 3.9.0 64 bit trên Windows 10 Pro 1903 64-bit.

Làm thế nào để bạn tìm thấy chỉ số cuối cùng của một số trong Python?

Hãy xem làm thế nào để tìm thấy sự xuất hiện cuối cùng của một phần tử bằng cách đảo ngược danh sách đã cho ...
Khởi tạo danh sách ..
Đảo ngược danh sách bằng phương pháp đảo ngược ..
Tìm chỉ mục của phần tử bằng phương thức chỉ mục ..
Chỉ số thực tế của phần tử là LEN (Danh sách) - INDEX - 1 ..
In chỉ số cuối cùng ..

Làm thế nào để tôi tìm thấy yếu tố đầu tiên và cuối cùng của một danh sách trong Python?

Phần tử đầu tiên được truy cập bằng cách sử dụng giá trị trống trước khi đại tràng đầu tiên và phần tử cuối cùng được truy cập bằng cách chỉ định Len () với -1 làm đầu vào..

Làm cách nào để có được chỉ số đầu tiên của một giá trị trong Python?

Làm thế nào để tìm chỉ mục của một yếu tố danh sách trong Python.Bạn có thể sử dụng phương thức Index () để tìm chỉ mục của phần tử đầu tiên khớp với một đối tượng tìm kiếm đã cho.Phương thức Index () trả về sự xuất hiện đầu tiên của một phần tử trong danh sách.use the index() method to find the index of the first element that matches with a given search object. The index() method returns the first occurrence of an element in the list.

Chỉ mục đầu tiên trong danh sách Python là gì?

Chỉ số danh sách bắt đầu với 0 trong Python.Vì vậy, giá trị chỉ số của 1 là 0, 'hai' là 1 và 3 là 2.0 in Python. So, the index value of 1 is 0, 'two' is 1 and 3 is 2.