Mã giải pháp cho câu hỏi trên
#let us declare a nested list and initialize it
nested_list = [['hockey','grapes','banana', 'orange'], [100, 500,800], "hello", 2.0, 5]
#lets start printing the value in index 3, the forst one which is given
print[nested_list[3]]#output should be 2.0
#printing the value at 1 2 i.e [1][2]
print[nested_list[1][2]]#output should be 800
#print the all values of index 0
print[nested_list[0]]#output should be ['hockey','grapes','banana', 'orange']
#printing the value at 0 3 i.e [0][3]
print[nested_list[0][3]]#output should be orange
#printing the value at 0 1 i.e [0][1]
print[nested_list[0][1]]#output should be grapes
Đầu ra chương trình mẫu
Tôi có danh sách trống này và tôi muốn vào một danh sách khác bên trong danh sách chính đây là lỗi mà tôi đang gặp:
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
Và đây là lỗi tôi đang gặp
IndexError: list assignment index out of range
Maik Lowrey
12.2k4 Huy hiệu vàng19 Huy hiệu bạc53 Huy hiệu đồng4 gold badges19 silver badges53 bronze badges
Hỏi ngày 12 tháng 12 năm 2021 lúc 13:57Dec 12, 2021 at 13:57
2
Thử cái này:
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
Lỗi là bạn đã cố gắng gán một yếu tố danh sách không tồn tại, sử dụng encend để thêm tên và điểm số
Đã trả lời ngày 12 tháng 12 năm 2021 lúc 14:07Dec 12, 2021 at 14:07
1
Trong Python, các danh sách có thể thay đổi vì các yếu tố của danh sách có thể được sửa đổi. Nhưng nếu bạn cố gắng sửa đổi một giá trị có chỉ mục lớn hơn hoặc bằng độ dài của danh sách thì bạn sẽ gặp chỉ mục chỉ số: danh sách chỉ mục gán ra khỏi phạm vi. & nbsp;
Python IndexError: Danh sách chỉ số phân công ra khỏi phạm vi ví dụ
Nếu 'trái cây' là một danh sách, trái cây = ['apple', 'chuối', 'ổi'] và bạn cố gắng sửa đổi trái cây [5] thì bạn sẽ gặp lỗi chỉ mục vì độ dài của danh sách trái cây = 3 ít hơn hơn chỉ số được yêu cầu sửa đổi là 5.
Python3
Các
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
6Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
7Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
8Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
1IndexError: list assignment index out of range
0IndexError: list assignment index out of range
1IndexError: list assignment index out of range
2IndexError: list assignment index out of range
3IndexError: list assignment index out of range
4=
IndexError: list assignment index out of range
6Output:
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range
Vì vậy, như bạn có thể thấy trong ví dụ trên, chúng tôi gặp lỗi khi chúng tôi cố gắng sửa đổi một chỉ mục không có trong danh sách các loại trái cây.
Python IndexError: Danh sách chỉ số phân công ra khỏi giải pháp phạm vi
Phương pháp 1: Sử dụng hàm Chèn []
Hàm chèn [chỉ mục, phần tử] có hai đối số, chỉ mục và phần tử và thêm một phần tử mới tại chỉ mục được chỉ định.
Hãy cùng xem cách bạn có thể thêm xoài vào danh sách các loại trái cây trên INDEX 1.
Python3
fruits
=
[
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
0____11Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
2Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
1____14Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
5Chỉ mục lồng nhau trong Python là gì?
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range0
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range1
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
1Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range3
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range4
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
6Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
7Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range7
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
9Output:
Original list: ['Apple', 'Banana', 'Guava'] Modified list: ['Apple', 'Mango', 'Banana', 'Guava']
Danh sách lồng nhau của Python là gì? Một danh sách có thể chứa bất kỳ đối tượng sắp xếp nào, ngay cả một danh sách khác [người phụ], từ đó có thể chứa chính những người phụ, v.v. Điều này được gọi là danh sách lồng nhau. Bạn có thể sử dụng chúng để sắp xếp dữ liệu vào các cấu trúc phân cấp.
Phương pháp 2: Sử dụng append []
Hàm append [phần tử] lấy một phần tử đối số và thêm một phần tử mới ở cuối danh sách.
Hãy để xem cách bạn có thể thêm xoài vào cuối danh sách bằng hàm append [phần tử].
Python3
fruits
=
[
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
0____11Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
2Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
1____14Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
5Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
6Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
7Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
8Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
9Original list: ['Apple', 'Banana', 'Guava'] Modified list: ['Apple', 'Banana', 'Guava', 'Mango']2
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range3
Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range4
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
6Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
Classroom.append[list[]]
Classroom[i][0] = name
Classroom[i][1] = score
print[Classroom]
7Traceback [most recent call last]: File "/example.py", line 3, in fruits[5]='Mango' IndexError: list assignment index out of range7
Classroom = []
n = int[input["give me the numbers of the students : "]]
for i in range[n] :
name = input["give me the name of the student : "]
score = float[input["give me the score of the student : "]]
l=[]
Classroom.append[l]
Classroom[i].append[name]
Classroom[i].append[score]
print[Classroom]
9Output:
Original list: ['Apple', 'Banana', 'Guava'] Modified list: ['Apple', 'Banana', 'Guava', 'Mango']