Hướng dẫn nested list indexing python assignment expert - danh sách lồng nhau lập chỉ mục chuyên gia phân công python

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]
6
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]
7
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]
8
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]
1
IndexError: list assignment index out of range
0
IndexError: list assignment index out of range
1

IndexError: list assignment index out of range
2
IndexError: list assignment index out of range
3
IndexError: list assignment index out of range
4=
IndexError: list assignment index out of range
6

Output:

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____11
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]
2
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]
1____14
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]
5

Chỉ 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 range
0
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
1
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]
1
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
3
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
4

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]
6
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]
7
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
7
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]
9

Output:

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____11
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]
2
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]
1____14
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]
5

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

Original 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 range
3
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
4

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]
6
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]
7
Traceback [most recent call last]:
 File "/example.py", line 3, in 
   fruits[5]='Mango'
IndexError: list assignment index out of range
7
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]
9

Output:

Original list: ['Apple', 'Banana', 'Guava']
Modified list: ['Apple', 'Banana', 'Guava', 'Mango']

Chỉ mục lồng nhau trong Python là gì?

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.A list can contain any sort object, even another list [sublist], which in turn can contain sublists themselves, and so on. This is known as nested list. You can use them to arrange data into hierarchical structures.

Làm cách nào để đọc một danh sách lồng nhau trong Python?

# Tạo danh sách ..
nestedList = [1, 2, ['a', 1], 3].
# Danh sách lập chỉ mục: Người đăng ký hiện đã được truy cập ..
Sublist = nestedList [2].
# Truy cập phần tử đầu tiên bên trong danh sách bên trong:.
phần tử = nestedList [2] [0].

Làm thế nào để bạn đếm các yếu tố trong một danh sách lồng nhau trong Python?

Chức năng tích hợp Len [] của lớp Danh sách có thể được sử dụng để đếm một số yếu tố trong danh sách danh sách. can be used to count a number of elements in the list of lists.

Danh sách lồng nhau giải thích với ví dụ trong Python là gì?

Danh sách lồng nhau là một danh sách trong danh sách.Python cung cấp các tính năng để xử lý danh sách lồng nhau một cách duyên dáng và áp dụng các chức năng phổ biến để thao túng các danh sách lồng nhau.Trong bài viết này, chúng ta sẽ thấy cách sử dụng danh sách hiểu để tạo và sử dụng các danh sách lồng nhau trong Python.

Bài Viết Liên Quan

Chủ Đề