按正确顺序删除 python 个列表

delete python list in the correct order

我试过按顺序删除:11,12,13,21,22,23,31,32,33 并保留一个空列表。 一开始我尝试了常规删除,但后来我明白你必须使用 int 进行删除,而你不能使用对象,所以我开始使用枚举函数,但我看到了另一个问题。 它被删除但不是整个列表只是其中的一部分。 有没有办法删除这个顺序?

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        jindex = jindex[j+1:]
    index = index[i+1:]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del jindex[j::]
    del b[i::]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del jindex[j]
    del index[i]
print(b)

print('\nnew try\n\n')

b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]

for i,index in enumerate(b):
    for j,jindex in enumerate(index):
        print(b)
        del b[i][j]
    del b[i]
print(b)

我的输出:

[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]

new try


[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[]

new try


[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[[], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
Traceback (most recent call last):
  File "/Users/asaf/PycharmProjects/first/openurl.py", line 28, in <module>
    del jindex[j]
IndexError: list assignment index out of range

Process finished with exit code 1

这就是我正在寻找的结果:

[[['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['21'],['22'],['23']],[['31'],['32'],['33']]]
[[['22'],['23']],[['31'],['32'],['33']]]
[[['23']],[['31'],['32'],['33']]]
[[['31'],['32'],['33']]]
[[['32'],['33']]]
[[['33']]]
[[]]

问题是您在修改列表时迭代它。这通常会导致您遇到的确切问题。相反,您必须遍历索引(更像是经典的 for 循环)并修改列表。但是请注意,您必须考虑到您要删除的索引与您迭代的索引不同。相反,您总是在删除子列表的第一个元素,然后在外循环中删除子列表本身(最后一次迭代除外)。

>>> b = [[['11'],['12'],['13']],[['21'],['22'],['23']],[['31'],['32'],['33']]]
>>> for sublength in [len(sub) for sub in b]:
...   for _ in range(sublength):
...     print(b)
...     del b[0][0]
...   if len(b) > 1: # or else you'll end up with []
...     del b[0]
... 
[[['11'], ['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['12'], ['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['13']], [['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['21'], ['22'], ['23']], [['31'], ['32'], ['33']]]
[[['22'], ['23']], [['31'], ['32'], ['33']]]
[[['23']], [['31'], ['32'], ['33']]]
[[['31'], ['32'], ['33']]]
[[['32'], ['33']]]
[[['33']]]
>>> print(b)
[[]]
>>>