英文:
Appending the list to the base list in Python
问题
J = [[2, 6, 9, 10]]
index = [[0, 3]]
J = [j for i, j in enumerate(J[0]) if i not in index[0]]
J.append(J)
英文:
I have a list J
. I am removing elements according to index
but I also want to append the new list to the base list J
. I present the current and expected outputs.
J = [[2, 6, 9, 10]]
index = [[0, 3]]
J = [j for i,j in enumerate(J[0]) if i not in index[0]]
print(J.append(J))
The current output is
None
The expected output is
[[2, 6, 9, 10],[6, 9]]
答案1
得分: 0
尝试这个:
J = [[2, 6, 9, 10]]
baseJ = J[0].copy()
index = [[0, 3]]
for i in range(len(index[0])):
baseJ.pop(index[0][i] - i)
J.append(baseJ)
print(J)
英文:
Try this:
J=[[2, 6, 9, 10]]
baseJ = J[0].copy()
index=[[0,3]]
for i in range(len(index[0])):
baseJ.pop(index[0][i] - i)
J.append(baseJ)
print(J)
答案2
得分: 0
如何在for循环中遍历新列表的值?
J = [[2, 6, 9, 10]]
index = [[0, 3]]
for i in index:
J.append(I)
抱歉,如果这不是你想要的!
英文:
How about looping through the values of the new list in a for loop?
J = [[2, 6, 9, 10]]
index = [[0, 3]]
for i in index:
J.append(I)
Sorry if this isn't what you wanted!
答案3
得分: 0
J = [[2, 6, 9, 10]]
index = [0, 3]
new_J = [j for i, j in enumerate(J[0]) if i not in index]
J.append(new_J)
print(J)
英文:
Try this:
J = [[2, 6, 9, 10]]
index = [0, 3]
new_J = [j for i, j in enumerate(J[0]) if i not in index]
J.append(new_J)
print(J)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论