In Python, how do I rotate a matrix 90 degrees counterclockwise?










0














>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""


Edit: Preferably without numpy.










share|improve this question























  • I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
    – DrM
    Nov 14 at 14:53











  • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
    – user10634718
    Nov 15 at 12:46










  • I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
    – DrM
    Nov 15 at 13:13















0














>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""


Edit: Preferably without numpy.










share|improve this question























  • I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
    – DrM
    Nov 14 at 14:53











  • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
    – user10634718
    Nov 15 at 12:46










  • I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
    – DrM
    Nov 15 at 13:13













0












0








0


0





>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""


Edit: Preferably without numpy.










share|improve this question















>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""


Edit: Preferably without numpy.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:22









Yvette Colomb

20.2k1369108




20.2k1369108










asked Nov 11 at 16:34









user10634718

141




141











  • I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
    – DrM
    Nov 14 at 14:53











  • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
    – user10634718
    Nov 15 at 12:46










  • I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
    – DrM
    Nov 15 at 13:13
















  • I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
    – DrM
    Nov 14 at 14:53











  • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
    – user10634718
    Nov 15 at 12:46










  • I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
    – DrM
    Nov 15 at 13:13















I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53





I answered your question - using python only, as you asked. If you agree that my answer is correct and faithful to the intent of your question, then please accept and/or up vote it. Or, please let me know what else you need, I am happy to try to help you. Thank you
– DrM
Nov 14 at 14:53













I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46




I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
– user10634718
Nov 15 at 12:46












I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13




I just copied the code from the answer and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
– DrM
Nov 15 at 13:13












2 Answers
2






active

oldest

votes


















2














You could use the numpy function rot90



import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])

def rotate_matrix(mat):
return np.rot90(mat)





share|improve this answer




























    1














    Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):



    new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


    If you want to do this in a function, then



    def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


    and either way, the result for



    m = [ [1,2,3], [2,3,3], [5,4,3]]


    is



    [[3, 3, 3], [2, 3, 4], [1, 2, 5]]


    Aside, if you want the usual transpose, then the simple one line pure python version is



    [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]





    share|improve this answer






















    • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
      – user10634718
      Nov 15 at 12:45










    • I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
      – DrM
      Nov 15 at 13:11










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250821%2fin-python-how-do-i-rotate-a-matrix-90-degrees-counterclockwise%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You could use the numpy function rot90



    import numpy as np
    m = np.array([[1,2,3],
    [2,3,3],
    [5,4,3]])

    def rotate_matrix(mat):
    return np.rot90(mat)





    share|improve this answer

























      2














      You could use the numpy function rot90



      import numpy as np
      m = np.array([[1,2,3],
      [2,3,3],
      [5,4,3]])

      def rotate_matrix(mat):
      return np.rot90(mat)





      share|improve this answer























        2












        2








        2






        You could use the numpy function rot90



        import numpy as np
        m = np.array([[1,2,3],
        [2,3,3],
        [5,4,3]])

        def rotate_matrix(mat):
        return np.rot90(mat)





        share|improve this answer












        You could use the numpy function rot90



        import numpy as np
        m = np.array([[1,2,3],
        [2,3,3],
        [5,4,3]])

        def rotate_matrix(mat):
        return np.rot90(mat)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:40









        Vikhyat Agarwal

        418214




        418214























            1














            Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):



            new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            If you want to do this in a function, then



            def rotate_matrix( m ):
            return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            and either way, the result for



            m = [ [1,2,3], [2,3,3], [5,4,3]]


            is



            [[3, 3, 3], [2, 3, 4], [1, 2, 5]]


            Aside, if you want the usual transpose, then the simple one line pure python version is



            [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]





            share|improve this answer






















            • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
              – user10634718
              Nov 15 at 12:45










            • I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
              – DrM
              Nov 15 at 13:11















            1














            Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):



            new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            If you want to do this in a function, then



            def rotate_matrix( m ):
            return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            and either way, the result for



            m = [ [1,2,3], [2,3,3], [5,4,3]]


            is



            [[3, 3, 3], [2, 3, 4], [1, 2, 5]]


            Aside, if you want the usual transpose, then the simple one line pure python version is



            [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]





            share|improve this answer






















            • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
              – user10634718
              Nov 15 at 12:45










            • I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
              – DrM
              Nov 15 at 13:11













            1












            1








            1






            Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):



            new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            If you want to do this in a function, then



            def rotate_matrix( m ):
            return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            and either way, the result for



            m = [ [1,2,3], [2,3,3], [5,4,3]]


            is



            [[3, 3, 3], [2, 3, 4], [1, 2, 5]]


            Aside, if you want the usual transpose, then the simple one line pure python version is



            [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]





            share|improve this answer














            Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):



            new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            If you want to do this in a function, then



            def rotate_matrix( m ):
            return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]


            and either way, the result for



            m = [ [1,2,3], [2,3,3], [5,4,3]]


            is



            [[3, 3, 3], [2, 3, 4], [1, 2, 5]]


            Aside, if you want the usual transpose, then the simple one line pure python version is



            [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 13:01

























            answered Nov 11 at 16:55









            DrM

            954314




            954314











            • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
              – user10634718
              Nov 15 at 12:45










            • I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
              – DrM
              Nov 15 at 13:11
















            • I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
              – user10634718
              Nov 15 at 12:45










            • I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
              – DrM
              Nov 15 at 13:11















            I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
            – user10634718
            Nov 15 at 12:45




            I've tried the code in python IDLE and for some reason, it's outputting the original matrix instead of rotating it. Can you please help?
            – user10634718
            Nov 15 at 12:45












            I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
            – DrM
            Nov 15 at 13:11




            I just copied the code from here and ran it, it is perfect. Please check that you are printing the right array in your code, c.f. print( new_matrix )
            – DrM
            Nov 15 at 13:11

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250821%2fin-python-how-do-i-rotate-a-matrix-90-degrees-counterclockwise%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus