Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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 旋转正方形_Python_Arrays_List_Rotation_Square - Fatal编程技术网

Python 旋转正方形

Python 旋转正方形,python,arrays,list,rotation,square,Python,Arrays,List,Rotation,Square,所以我要建立的是一个程序,它取一个3x3的正方形,在给定的指令下旋转它。例如,如果我有3x3平方米 0 5 2 7 8 4 1 6 3 旋转就是这些 U 2 D 2 L 1 D 2 我旋转它:在第二列上 0 5 4 7 8 3 1 6 2 在2号 0 5 2 7 8 4 1 6 3 第一排左边-不确定是右边,但 0 5 2 8 4 7 1 6 3 在第二排 0

所以我要建立的是一个程序,它取一个3x3的正方形,在给定的指令下旋转它。例如,如果我有3x3平方米

0 5 2   
7 8 4   
1 6 3 
旋转就是这些

U 2
D 2
L 1
D 2
我旋转它:在第二列上

0   5   4   
7   8   3   
1   6   2
在2号

0   5   2   
7   8   4   
1   6   3 
第一排左边-不确定是右边,但

0   5   2   
8   4   7   
1   6   3 
在第二排

0 5 3
8 4 2
1 6 7
最后一轮应该是

0 5 3
8 4 2
1 6 7
下面是我的程序,我已经设法运行了这个文件并生成了一个3x3,但我不知道如何移动正方形,如果有人能帮助我的话,我会非常感激地指出如何开始移动

def readfile(x):
    list=[]
    file= open(x)
    count=0
    maxcount=0
    while True:
        line = file.readline()
        if count<3:
            line=line.rstrip('\n').split(' ')
            x=[]
            for i in line:
                x.append(int(i))
            list.append(x)
            count+=1
        elif count==3:
            maxcount=int(line.rstrip('\n'))
            for i in range(count):
                for j in range(count):
                    print(list[i][j],' ',end=' ')
                print()
            print(maxcount)
            count+=1
        elif maxcount>0:
            line=line.rstrip('\n')
            lines=line.split(' ')
            print(" ".join(lines))
            maxcount-=1
readfile("file.txt")
def读取文件(x):
列表=[]
文件=打开(x)
计数=0
最大计数=0
尽管如此:
line=file.readline()
如果计数为0:
line=line.rstrip('\n')
行=行分割(“”)
打印(“.”连接(行))
最大计数-=1
readfile(“file.txt”)

我定义了两个函数来执行这些操作。您可以传递right=True或up=True,还有一个可选参数,可通过该参数旋转n步

def rotate_row_elements(matrix, row,by, right=True):
    if right:
        for i in range(by):
            matrix[row] = [matrix[row].pop(), *matrix[row]]
    else:
        for i in range(by):
            matrix[row] = [*matrix[row][1:], matrix[row][0]]

def rotate_col_elements(matrix, col,by, down=True):
    if down:
        temp = [matrix[j][col] for j in range(len(matrix))]
        
        for i in range(by):
            temp = [temp.pop(), *temp]
            
        for j in range(len(matrix)):
            matrix[j][col] = temp[j]
            
    else:
        
        temp = [matrix[j][col] for j in range(len(matrix))]
        for i in range(by):
            temp = temp[1:] + [temp[0]]
        
        for j in range(len(matrix)):
            matrix[j][col] = temp[j]        


matrix = [
    
[0 ,5, 2],   
[7 ,8 ,4  ] ,
[1, 6, 3 ]]
rotate_col_elements(matrix, 1, 2)
print(matrix)

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

如果您不介意导入numpy软件包,我认为
np.roll()
可能有效。请修复有关行的问题措辞。有些“行”实际上是列。例如,列向上/向下旋转,行向左/向右旋转。还要注意,提供的代码不会终止。不要将任何变量命名为“list”,它是内置类型的名称
list
,将覆盖它。虽然与您的问题无关,但它可能会在代码中导致许多问题。@MichaelRuth对“行”表示抱歉,感谢您告知变量列表。我相信当你调用函数readfile和它自己的文件,即正方形和rotations@j1-李:我没有义务使用任何图书馆。我认为旋转是错误的
UP
DOWN
列的旋转给出相同的输出。我想应该是
UP
shift和
DOWN
shift