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

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

Appending the list to the base list in Python

问题

  1. J = [[2, 6, 9, 10]]
  2. index = [[0, 3]]
  3. J = [j for i, j in enumerate(J[0]) if i not in index[0]]
  4. 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.

  1. J = [[2, 6, 9, 10]]
  2. index = [[0, 3]]
  3. J = [j for i,j in enumerate(J[0]) if i not in index[0]]
  4. print(J.append(J))

The current output is

  1. None

The expected output is

  1. [[2, 6, 9, 10],[6, 9]]

答案1

得分: 0

尝试这个:

  1. J = [[2, 6, 9, 10]]
  2. baseJ = J[0].copy()
  3. index = [[0, 3]]
  4. for i in range(len(index[0])):
  5. baseJ.pop(index[0][i] - i)
  6. J.append(baseJ)
  7. print(J)
英文:

Try this:

  1. J=[[2, 6, 9, 10]]
  2. baseJ = J[0].copy()
  3. index=[[0,3]]
  4. for i in range(len(index[0])):
  5. baseJ.pop(index[0][i] - i)
  6. J.append(baseJ)
  7. print(J)

答案2

得分: 0

如何在for循环中遍历新列表的值?

  1. J = [[2, 6, 9, 10]]
  2. index = [[0, 3]]
  3. for i in index:
  4. J.append(I)

抱歉,如果这不是你想要的!

英文:

How about looping through the values of the new list in a for loop?

  1. J = [[2, 6, 9, 10]]
  2. index = [[0, 3]]
  3. for i in index:
  4. J.append(I)

Sorry if this isn't what you wanted!

答案3

得分: 0

  1. J = [[2, 6, 9, 10]]
  2. index = [0, 3]
  3. new_J = [j for i, j in enumerate(J[0]) if i not in index]
  4. J.append(new_J)
  5. print(J)
英文:

Try this:

  1. J = [[2, 6, 9, 10]]
  2. index = [0, 3]
  3. new_J = [j for i, j in enumerate(J[0]) if i not in index]
  4. J.append(new_J)
  5. 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:

确定