英文:
Issue with appending a list in Python
问题
以下是您要的代码翻译部分:
我有一个空列表```J```。我正在编写一个循环,想要根据```t```的值进行附加操作。但是我遇到了一个错误。我展示了预期的输出。
J=[]
for t in range(0,1):
J[t]=[[1,3,5]]
J[t+1]=[[1,3]]
J.append([[J[t]],[J[t+1]]])
print(J)
错误是
在<module>中
J[t]=[[1,3,5]]
IndexError: list assignment index out of range
预期的输出是
[[1,3,5],[1,3]]
英文:
I have an empty list J. I am writing a loop and want to append based on the t values. But I am getting an error. I present the expected output.
J=[]
for t in range(0,1):
J[t]=[[1,3,5]]
J[t+1]=[[1,3]]
J.append([[J[t]],[J[t+1]]])
print(J)
The error is
in <module>
J[t]=[[1,3,5]]
IndexError: list assignment index out of range
The expected output is
[[1,3,5],[1,3]]
答案1
得分: 1
By specifying J[t] = [...] you are trying to assign the array to J[t] which doesn't exist yet.
你指定J[t] = [...]时,你试图将数组赋给J[t],但J[t]尚不存在。
You are also then trying to take list items and add them back into the list again; even if J[t] = [...] were working, this would double up your results.
你还尝试将列表项再次添加到列表中;即使J[t] = [...]能够工作,这也会导致结果重复。
Instead, try this:
相反,尝试这样做:
J=[]
for t in range(0,1):
J.append([1,3,5])
J.append([1,3])
print(J)
This produces your expected result.
这会生成你期望的结果。
It's worth noting that for t in range(0,1) is meaningless, as t will only ever be zero; If you change to range(0,2) you would get:
值得注意的是,for t in range(0,1)是无意义的,因为t永远只会是零;如果你改为range(0,2),你将得到:
[[1,3,5],[1,3],[1,3,5],[1,3]] which may be what you're after? It's difficult to ascertain why you are using the for loop from your example.
这可能是你想要的吗?很难确定你为什么要使用你示例中的for循环。
英文:
By specifying J[t] = [...] you are trying to assign the array to J[t] which doesn't exist yet.
You are also then trying to take list items and add them back into the list again; even if J[t] = [...] were working, this would double up your results.
Instead, try this:
J=[]
for t in range(0,1):
J.append([1,3,5])
J.append([1,3])
print(J)
This produces your expected result.
It's worth noting that for t in range(0,1) is meaningless, as t will only ever be zero; If you change to range(0,2) you would get:
[[1,3,5],[1,3],[1,3,5],[1,3]] which may be what you're after? It's difficult ascertain why you are using the for loop from your example.
答案2
得分: 1
在for循环中,当t==0时,您尝试访问J中的索引t,这等同于J[0],但是J是[],没有可索引的条目0。
如果您希望输出为[[1,3,5],[1,3]],可以使用for循环,可以参考Mureinik的答案,或者考虑以下代码:
J = []
odds = [1,3,5]
for t in range(0,2):
J.append(odds[:(len(odds))-t])
print(J)
英文:
In the for loop when t==0, you try to access the index t in J, which comes down to J[0], however J is [], which doesn't have an index-able entry 0.
If you want [[1,3,5],[1,3]] as the output, using a for loop, have a look at Mureinik's answer, or consider:
J = []
odds = [1,3,5]
for t in range(0,2):
J.append(odds[:(len(odds))-t])
print(J)
答案3
得分: 0
你不能给一个尚不存在的列表索引分配值。J 被初始化为空列表,因此从定义上来说它没有索引,对任何索引的任何赋值都会导致 IndexError。相反,你应该只是将值附加到它上面:
J.append([1,3,5]) # 索引 0
J.append([1,3]) # 索引 1
英文:
You can't assign a value to an index of the list that isn't there yet. J is initialized as an empty list, so by definition it has no indexes, and any assignment to any index will result in an IndexError. Instead, you should just append to it:
J.append([1,3,5]) # Index 0
J.append([1,3]) # Index 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论