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 range2
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range0
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range1
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range0
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range6
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range8
đầ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 range9
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range10
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range13
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range0
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range15
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range0
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range17
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range19
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range10
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range11
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range12
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range13
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range14
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range15
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range16
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range18
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range19
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range91
đầ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 range9
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range10
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range96
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range97
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range17
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range19
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range10
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range11
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range12
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range13
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range14
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range36
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range19
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range91
đầu ra
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range1
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 range9
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range10
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range3
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range96
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range97
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range17
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range4
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range19
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range29
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range11
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range12
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range15
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range16
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range8
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range16
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range5
Traceback [most recent call last]: File "/home/6536be743dacb18c2fcc724dd5aa21c6.py", line 5, in print[names[name]] IndexError: list index out of range18
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