Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:顺时针方向的2D到1D列表?_Python_List_Rotation - Fatal编程技术网

Python:顺时针方向的2D到1D列表?

Python:顺时针方向的2D到1D列表?,python,list,rotation,Python,List,Rotation,我对Python有点生疏,我正在寻找一个函数,它将任何nxn二维列表转换为顺时针方向的1D列表 例如: 当n=3时 list=[[2,3,5]、[8,7,1]、[0,4,6]] 或 将成为 result = [2, 3, 5, 1, 6, 4, 0, 8, 7] 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=5时 list=[[2,3

我对Python有点生疏,我正在寻找一个函数,它将任何nxn二维列表转换为顺时针方向的1D列表

例如:

当n=3时

list=[[2,3,5]、[8,7,1]、[0,4,6]]

将成为

result = [2, 3, 5, 1, 6, 4, 0, 8, 7]
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=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]

将成为

result = [2, 3, 5, 1, 6, 4, 0, 8, 7]
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]
对于nxn的任何值,如何有效地执行该操作

改编自

lists = [[2, 3, 5],[ 8, 7, 1],[ 0, 4, 6]]
output_list = [item for list in lists for item in list]

这听起来像是一道考试题,是吗

这将是我使用递归和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
这也适用于:

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)
从数学导入楼层
列表=[[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(列表)#假设每个列表也有n个长度
输出列表=[]
idx=0
而idx
打印(“跨->”)
对于列表中的项目[idx][idx:n-idx]:
输出列表。追加(项)
打印(输出列表)
如果len(输出列表)=n*n:
打破
#向下
打印(“向下”)
对于范围内的_idx(idx+1,n-idx-1):
输出\u list.append(列表[\u idx][n-idx-1])
打印(输出列表)
如果len(输出列表)=n*n:
打破
#穿过
此方法从数组外部打印到数组内部。例如,数组是:

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 16 11 6

然后,处理内部阵列(3x3):

按时钟方向打印3x3阵列的外部,得到7 8 9 14 19 18 17 12


最后,处理13个问题。

看,这就是重点。您只将2D列表转换为1D列表(缺少顺时针转换点),是否正在寻找适用于任何nxn列表的内容?或者只是3x3@JackEvans对于任何nxn:”),您能给出不同大小列表中顺时针旋转的其他示例吗?@JackEvans我添加了更多细节。您可以看到。:')可能的复制品可以用几种方法清理,但逻辑是存在的。没问题。谢谢:’)谢谢。谢谢:’)不,这是我的项目之一;本地二进制模式算法需要它。不过非常感谢您的帮助。:')
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
7  8  9
12 13 14
17 18 19