我是Python的新手,我正在寻找能够转换 n x 的功能 n 在 顺时针方向 中将2D列表列入一维列表 。
例如:
当n = 3
时 list = [[2, 3, 5],[ 8, 7, 1],[ 0, 4, 6]]
或
list = [[2, 3, 5]
,[8, 7, 1]
,[0, 4, 6]]
会变成
result = [2, 3, 5, 1, 6, 4, 0, 8, 7]
当n = 5时
list = [[2, 3, 5, 9, 10],[ 8, 7, 1, 11, 13],[ 0, 4, 6, 21, 22], [12, 19, 17, 18, 25], [14, 15, 16, 23, 24]]
或
list = [[ 2, 3, 5, 9, 10]
,[ 8, 7, 1, 11, 13]
,[ 0, 4, 6, 21, 22]
,[ 12, 19, 17, 18, 25]
, [ 14, 15, 16, 23, 24]]
会变成
result = [2, 3, 5, 9, 10, 13, 22, 25, 24, 23, 16, 15, 14, 12, 0, 8, 7, 1, 11, 21, 18, 17, 19, 4, 6]
我怎样才能有效地为 n x n 的任何值做到这一点?
答案 0 :(得分:1)
改编自Print two-dimensional array in spiral order
First let's print the whole file:
this is line1.Say hello.
this is line2. This must be printed!!
this is line3.This is cool!Print please
now let's rewind, kind of like tape.
Let's print three lines:
1 this is line1.Say hello.
2
3 this is line2. This must be printed!!
答案 1 :(得分:1)
这听起来像是考试题 - 是吗?!
这将是我使用递归和Python的列表操作运算符的解决方案:
def clockwise(input_list, output_list):
list_size = len(input_list[0])
if list_size == 1:
output_list.append(input_list[0][0])
else:
for i in range(list_size):
output_list.append(input_list[0][i])
for i in range(list_size)[1:]:
output_list.append(input_list[i][list_size - 1])
for i in reversed(range(list_size)[:-1]):
output_list.append(input_list[list_size - 1][i])
for i in reversed(range(list_size)[1:-1]):
output_list.append(input_list[i][0])
new_list = list()
for i in range(list_size - 2):
new_list.append(input_list[i + 1][1:-1])
return clockwise(new_list, output_list)
l = [[2, 3, 5, 9, 10],[ 8, 7, 1, 11, 13],[ 0, 4, 6, 21, 22], [12, 19, 17, 18, 25], [14, 15, 16, 23, 24]]
output_list = []
clockwise(l, output_list)
print output_list
答案 2 :(得分:1)
这也有效:
from math import floor
lists = [[ 2, 3, 5, 9, 10]
,[ 8, 7, 1, 11, 13]
,[ 0, 4, 6, 21, 22]
,[ 12, 19, 17, 18, 25]
, [ 14, 15, 16, 23, 24]]
n = len(lists) # assume each list also has n-length
output_list = []
idx = 0
while idx <= floor(n/2):
if len(output_list) == n*n:
break
# across ->
print("Across ->")
for item in lists[idx][idx:n-idx]:
output_list.append(item)
print(output_list)
if len(output_list) == n*n:
break
# down
print("Down")
for _idx in range(idx+1, n-idx-1):
output_list.append(lists[_idx][n-idx-1])
print(output_list)
if len(output_list) == n*n:
break
# across <-
print("Across <-")
for _idx in range(n-idx-1, idx-1, -1):
output_list.append(lists[n-idx-1][_idx])
print(output_list)
if len(output_list) == n*n:
break
# up
print("Up")
for _idx in range(n-idx-2, idx, -1):
output_list.append(lists[_idx][idx])
print(output_list)
idx += 1
print("\nOutput: ")
print(output_list)
答案 3 :(得分:1)
import math
import numpy as np
def print_wall(input, result):
n = input.shape[0]
for i in range(n): # print top outer
result.append(input[0][i])
for i in range(n - 1): # print right outer
result.append(input[i + 1][n - 1])
for i in range(n - 1): # print bottom outer
result.append(input[n - 1][n - 2 - i])
for i in range(n - 2): # print left outer
result.append(input[n - 2 - i][0])
def clock_wise(input):
n = input.shape[0]
result = list()
for i in range(math.ceil(n / 2)): # from the outer to the inner
print_wall(input[i: n - i, i: n - i], result)
print(result)
此方法从数组的外部打印到数组的内部。例如,数组是:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
首先,按顺时针方向打印5x5阵列的外部,得到1 2 3 4 5 10 15 20 25 24 23 22 21 16 11 6;
然后,处理内部数组(3x3):
7 8 9
12 13 14
17 18 19
以顺时针方向打印3x3阵列的外部,得到7 8 9 14 19 18 17 12;
最后,处理13。
答案 4 :(得分:0)
lists = [[2, 3, 5],[ 8, 7, 1],[ 0, 4, 6]]
output_list = [item for list in lists for item in list]