python打印二维数组_Python 顺时针顺序打印一个二维数组

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.csdn.net/buxinchun/article/details/80255907?utm_source=blogxgwz0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值