以正确的顺序删除python列表

时间:2016-08-08 17:27:45

标签: python list del

我试图按此顺序删除: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']]]
[[]]

1 个答案:

答案 0 :(得分:0)

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

>>> 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)
[[]]
>>>