def clockwisePrint(matrix):
res = []
while matrix:
res += matrix.pop(0)
if matrix:
index = 0
for row in matrix:
res.append(row.pop()) # only pop element
if not row: #delete empty row in case [[],[],[]]
matrix.pop(index)
index += 1
if matrix:
res += matrix.pop()[::-1]
if matrix:
index = len(matrix)
for row in matrix[::-1]:
index -= 1
res.append(row.pop(0))
if not row:
matrix.pop(index)
return res
def antiClockwisePrint(matrix):
res = []
while matrix:
index = 0
for row in matrix:
res.append(row.pop(0))
if not row:
matrix.pop(index)
index += 1
if matrix:
res += matrix.pop()
python顺时针(逆时针)打印列表
最新推荐文章于 2022-11-01 17:14:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)