从列表中删除特定元素并在Python中追加元素。

huangapple go评论71阅读模式
英文:

Removing specific elements from a list and appending in Python

问题

I have a list J[0]. I am removing [1] from this list and creating J[1]. Now I wish to append J[0],J[1] into J. But I am getting an error. I present the expected output.

J=[]
for t in range(0,2):
    J.append([1, 3, 7, 9])
    J[1]=J[0][1:]
    
print("New J:", J[t])

The error is

in <module>
    J[0] = [1, 3, 7, 9]

IndexError: list assignment index out of range

The expected output is

[[1, 3, 7, 9],[3, 7, 9]]
英文:

I have a list J[0]. I am removing [1] from this list and creating J[1]. Now I wish to append J[0],J[1] into J. But I am getting an error. I present the expected output.

J=[]
for t in range(0,2):
    J[0] = [[1, 3, 7, 9]]
    J[1]=J.remove([1])
    
print(&quot;New J:&quot;, J[t])

The error is

in &lt;module&gt;
    J[0] = [[1, 3, 7, 9]]

IndexError: list assignment index out of range

The expected output is

[[1, 3, 7, 9],[3, 7, 9]]

答案1

得分: 1

Here is the translated content:

由于您已经将 J 初始化为空列表,因此无法访问 J[0] 元素,这是IndexError的原因。您首先必须添加新元素到 J 中才能访问它们。

使您的代码工作的方法如下:

J = list()
l = [1, 3, 7, 9]     # 将列表赋值给变量 l
l_removed = l.copy() # 复制 l 以避免操作它
J.append(l)          # 将 l 添加到 J
l_removed.remove(1)  # 从复制的列表中删除 1
J.append(l_removed)  # 将新列表添加到 J
print(J)

这是上述代码块的输出:

[[1, 3, 7, 9], [3, 7, 9]]
英文:

Since you have initialized J to be an empty list, you cannot access the J[0] element and that is the reason for the IndexError. You first have to append new elements to J in order to access them.

The way to make your code work could be as follows:

J=list()
l = [1, 3, 7, 9]     # Assigning list to variable l
l_removed = l.copy() # Making a copy of l in order to avoid manipulating it
J.append(l)          # Appending l to J
l_removed.remove(1)  # Removing the 1 from the copied list
J.append(l_removed)  # Appending the new list to J
print(J)

Here is the output of the above code block:

[[1, 3, 7, 9], [3, 7, 9]]

答案2

得分: 1

I think you are getting confused by the levels of nested lists and by indexes. Eg. You are starting with an empty list J. You assign then a list containing a list to the zeroth entry of that list.
You are trying to remove a list containing 1 from J, whivh is not there.
You are trying to do this two times with the for loop.

I guess what you want, could be something like this:

j = [[1, 3, 7, 9]]
j_new = j[0].copy()
j_new.pop(0)
j.append(j_new)
print(j)
英文:

I think you are getting confused by the levels of nested lists and by indexes. Eg. You are starting with an empty list J. You assign then a list containing a list to the zeroth entry of that list.
You are trying to remove a list containing 1 from J, whivh is not there.
You are trying to do this two times with the for loop.

I guess what you want, could be something like this:

j = [[1, 3, 7, 9]]
j_new = j[0].copy()
j_new.pop(0)
j.append(j_new)
print(j)

答案3

得分: 0

以下是您要翻译的代码部分:

你可以这样做

J = []

for _ in range(2):
    J.append([1, 3, 7, 9])

J[-1].pop(0)

print(J)

**输出**

[[1, 3, 7, 9], [3, 7, 9]]

**或者**

而不是删除元素不要将其放入列表中 - 例如

J = []

for i in range(2):
    J.append([1, 3, 7, 9][i:])

print(J)
英文:

You could do it like this:

J = []

for _ in range(2):
    J.append([1, 3, 7, 9])

J[-1].pop(0)

print(J)

Output:

[[1, 3, 7, 9], [3, 7, 9]]

Or:

Rather than deleting the element, don't put it in the list - e.g.,

J = []

for i in range(2):
    J.append([1, 3, 7, 9][i:])

print(J)

huangapple
  • 本文由 发表于 2023年5月21日 17:43:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定