简体   繁体   English

Python错误:“IndexError:列表索引超出范围”

[英]Python error : "IndexError: list index out of range"

I'm trying to put some data to my "nested list".我正在尝试将一些数据放入我的“嵌套列表”。 I'm a beginner, please explain me, thank you all.我是初学者,请解释一下,谢谢大家。

n_list = []
n = int(input())
for i in range (0,n):
    print(i)
    name = input()
    n_list[i].append(name)
    val = int(input())
    n_list[i].append(val)
print(n_list)

If you're appending to list, you don't need to use index (by using n_list[i] you're pointing to i-th element of the array):如果您要附加到列表,则不需要使用索引(通过使用n_list[i]您指向数组的第 i 个元素):

n_list = []
n = int(input())
for i in range (0,n):
    print(i)
    name = input()
    n_list.append(name)
    val = int(input())
    n_list.append(val)
print(n_list)

I you want to nest lists, do this:我要嵌套列表,请执行以下操作:

n_list = []
n = int(input())
for i in range (0,n):
    print(i)
    inner_list = []
    name = input()
    inner_list.append(name)
    val = int(input())
    inner_list.append(val)
    n_list.append(inner_list)
print(n_list)

The mistake is pretty simple and expected, so nothing to feel bad about.这个错误非常简单且在意料之中,所以没什么好难过的。

The append function works in the following way (syntax):
    <list_name>.append(<item>)

is the name of the list and is the item to be inserted.是列表的名称,是要插入的项目。

In your code,在您的代码中,

n_list[i].append(name)

You are trying to append to the list present in i th index of n_list, which doesn't exist.您正在尝试附加到 n_list 的第 i 个索引中存在的列表,该索引不存在。 So its showing the error所以它显示错误

Like Andronicus said you dont need to use the index [i].就像 Andronicus 说你不需要使用索引 [i]。 But you are talking about a 'nested list'.但是您正在谈论“嵌套列表”。 What you are trying to do is not 'nested'.您要做的不是“嵌套”。 Seems like you want to create a list of key/value objects:似乎您想创建一个键/值对象列表:

n_list = []
n = int(input())
for i in range (0,n):
    print(i)
    name = input()
    val = int(input())
    n_list.append([name, val])
print(n_list)

Results in something like: [['myname', 2], ['yourname', 3]] or as a dictionary:结果类似于: [['myname', 2], ['yourname', 3]]或作为字典:

n_list = []
n = int(input())
for i in range (0,n):
    print(i)
    name = input()
    val = int(input())
    n_list.append({'name': name, 'value': val})
print(n_list)

Results in something like: [{'name': 'myname', 'value': 3}, {'name': 'yourname', 'value': 4}]结果如下: [{'name': 'myname', 'value': 3}, {'name': 'yourname', 'value': 4}]

The problem is that you are trying to access the i -th item of an empty list.问题是您正在尝试访问列表的第i个项目。 Since the list has 0 elements and you are asking for the i -th one, you get an error.由于列表有 0 个元素并且您要求第i个元素,因此您会收到错误消息。

You could initialize a list with dummy values您可以使用虚拟值初始化列表

n = int(input())
n_list = n * [None]

and then, write然后,写

n_list[i] = the_value

This one is working.这个正在工作。 the reasons why your method didn't work it's because you were identifying an index that doesn't yet exist您的方法不起作用的原因是因为您正在识别尚不存在的索引

n_list[i]

the list has no elements.该列表没有元素。 that's why you need to append() the new ones.这就是为什么你需要 append() 新的。

n_list = []
n = int(input("write list length: \n"))
for i in range (n):
    item = [] #create the nested list for each loop

    name = input("Write name : \n")
    item.append(name)

    val = int(input("write value : \n"))
    item.append(val)

    n_list.append(item)

print(n_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM