Chỉ mục danh sách nằm ngoài phạm vi trong python là gì?

Vấn đề “chỉ mục danh sách nằm ngoài phạm vi của bạn” có thể là vấn đề bạn đã gặp phải nếu bạn đã từng làm việc với danh sách. Mặc dù sự cố này xảy ra thường xuyên nhưng lập trình viên mới có thể khó chẩn đoán

Cách tăng chỉ mục danh sách ra khỏi phạm vi

ví dụ 1.  

Ở đây danh sách của chúng tôi là 3 và chúng tôi đang in với kích thước 4, vì vậy trong trường hợp này, nó sẽ tạo ra một danh sách chỉ mục nằm ngoài phạm vi

Python3




Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
2
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
0
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
1
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
0
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
6
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
8

đầu ra

IndexError: list index out of range

ví dụ 2

Ở đây chúng tôi đang in cùng một mẫu mà không có chỉ mục

Python3




Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
9

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
10
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
13
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
0
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
15
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
0
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
17
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4

 

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
19
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
10
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
11
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
12
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
13
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
14
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
15
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
16
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
18

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
19
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
91

đầu ra

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range

Cách giải chỉ mục danh sách ngoài phạm vi

  • Sử dụng phạm vi[]
  • Sử dụng suy nghĩ kết thúc
  • Sử dụng Chỉ mục []

Phương pháp 1. Sử dụng phạm vi[]

Phạm vi được sử dụng để đưa ra một phạm vi cụ thể, hàm phạm vi Python [] trả về chuỗi số đã cho giữa phạm vi đã cho

Cú pháp của phạm vi[]

phạm vi [bắt đầu, dừng, bước]

  • bắt đầu. Một số tùy chọn xác định phần đầu của chuỗi. 0 là giá trị mặc định nếu để trống
  • dừng lại. Một số nguyên chỉ định điểm mà tại đó chuỗi sẽ kết thúc
  • bước chân-. tùy chọn và cần thiết nếu bạn muốn tăng mức tăng hơn 1. 1 là giá trị mặc định

Tham số dừng cho hàm range[] là giá trị được trả về bởi hàm len[]

Ví dụ về danh sách sửa lỗi ngoài phạm vi

Python3




Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
9

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
10
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
96
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
97
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
17
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4

 

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
19
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
10
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
11
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
12
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
13
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
14
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
36

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
19
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
91

đầu ra

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
1

Phương pháp 2. Sử dụng trong toán tử

Python3




Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
9

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
10
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
3
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
96
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
97
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
17
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
4

 

Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
19
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
29
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
11
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
12
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
15
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
16
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
8
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
16
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
5
Traceback [most recent call last]:
 File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in 
   print[names[name]]
IndexError: list index out of range
18

Chỉ mục danh sách trong Python là gì?

Phương thức index[] của Python giúp bạn tìm vị trí chỉ mục của một phần tử hoặc một mục trong một chuỗi ký tự hoặc danh sách các mục. It spits out the lowest possible index of the specified element in the list. In case the specified item does not exist in the list, a ValueError is returned.

Nó có nghĩa là gì bởi chỉ mục ngoài phạm vi?

Nói chung, chỉ mục danh sách nằm ngoài phạm vi có nghĩa là bạn đang cung cấp chỉ mục mà phần tử danh sách không tồn tại .

Ý nghĩa của chỉ mục danh sách nằm ngoài phạm vi trong Python là gì?

Trong bài viết này, chúng ta đã nói về IndexError. lỗi liệt kê chỉ mục ngoài phạm vi trong Python. Lỗi này thường xảy ra khi chúng tôi cố gắng truy cập một mục trong danh sách bằng cách sử dụng một chỉ mục không tồn tại trong danh sách

Chủ Đề