我想颠倒顺序来反转:
但这不是个好主意,我想用,我该怎么做?
答案 0 :(得分:1)
>>> listA = [[13, 22, 33,],[4,5, 6],[7,8, 9]]
>>> [list(reversed(inner_list)) for inner_list in reversed(listA)]
[[9, 8, 7], [6, 5, 4], [33, 22, 13]]
答案 1 :(得分:0)
使用它来反转外部和内部列表:
def reverse_list(x):
return list(y[::-1] for y in x[::-1])
这是样本输出:
>>> reverse_list([[1,'a','b'],[2,'foo',4],[3,'baz']])
[['baz', 3], [4, 'foo', 2], ['b', 'a', 1]]
如果真的想要手动执行(你不应该这样,因为它不是Pythonic),你可以按照以下方式进行:
def reverse_list(x):
old = x[:] # Copy list
outer = []
while old: # While old is not empty
elem = old.pop()
inner = []
while elem: # While elem is not empty
inner.append(elem.pop())
outer.append(inner)
return outer