打印给定数组的所有对角线元素
to print all the diagonal elements for a given array
a = [['a','b','c'],
['d','e','f','g'],
['h','i','j','k'],
['l','m','n']]
我需要打印给定数组的对角线元素,例如输出为:
[['l'],['h','m'],['d','i','n'],['a','e','j'],['b','f','k'],['c','g']]
我猜大概是这样的:
a = [
['a','b','c'],
['d','e','f','g'],
['h','i','j','k'],
['l','m','n']
]
d = 0
while True:
array = []
j = (len(a)-1)-d
k = 0
if j<0:
k= -j
j = 0
while j<len(a) and k<len(a[j]):
array.append(a[j][k])
j+=1
k+=1
if len(array) == 0:
break
print(array)
d+=1
a = [['a','b','c'],
['d','e','f','g'],
['h','i','j','k'],
['l','m','n']]
我需要打印给定数组的对角线元素,例如输出为:
[['l'],['h','m'],['d','i','n'],['a','e','j'],['b','f','k'],['c','g']]
我猜大概是这样的:
a = [
['a','b','c'],
['d','e','f','g'],
['h','i','j','k'],
['l','m','n']
]
d = 0
while True:
array = []
j = (len(a)-1)-d
k = 0
if j<0:
k= -j
j = 0
while j<len(a) and k<len(a[j]):
array.append(a[j][k])
j+=1
k+=1
if len(array) == 0:
break
print(array)
d+=1