def initMatrix(dimx, dimy):
"""构建二维数组"""
matrix = [['0' for y in range(dimy)] for x in range(dimx)]
for x in range(dimx):
for y in range(dimy):
matrix[x][y] = '{}{}'.format(x, y)
return matrix
def printMatrix(ma):
for i in range(len(ma)):
print(ma[i])
def boBaocai(newList, ma, startX, startY, lastX, lastY):
"""newList可以是任意类型,看传入的是什么类型"""
tempList = []
for i in range(lastY - startY + 1):
tempList.append(ma[startX][startY + i])
startX = startX + 1
for j in range(lastX - startX + 1):
tempList.append(ma[startX + j][lastY])
lastY = lastY - 1
for k in range(lastY - startY + 1):
tempList.append(ma[lastX][lastY - k])
lastX = lastX - 1
for m in range(lastX - startX + 1):
tempList.append(ma[lastX - m][startY])
startY = startY + 1
newList.extend(tempList)
while len(newList) < len(ma) * len(ma[0]):
boBaocai(newList, ma, startX, startY, lastX, lastY)
return newList
mBaocai = initMatrix(6, 6)
print("---------print initMatrix------------")
printMatrix(mBaocai)
kickout = []
boBaocai(kickout, mBaocai, 0, 0, len(mBaocai) - 1, len(mBaocai[0]) - 1)
print("----------after operate--------------")
print(kickout)
转载地址:https://blog.youkuaiyun.com/buxinchun/article/details/80255907?utm_source=blogxgwz0