在Python中将列表附加到基本列表中。

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

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)

huangapple
  • 本文由 发表于 2023年3月1日 14:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600258.html
匿名

发表评论

匿名网友

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

确定