Rotating a matrix counter-clockwise changes the original as well










2














So I currently have a function to rotate a matrix counter-clockwise, the list is always a square grid:



def rotate(m: List[List[int]]) -> None:
temp = m.copy()

if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]

m = temp.copy()


I've traced this function repeatedly and to my knowledge it should be working. The problem is that for some reason every change to temp affects the original list. For example,



ORIGINAL =

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

WHAT SHOULD OUTPUT =

[[3, 6, 9],

[2, 5, 8],

[1, 4, 7]]

WHAT ACTUALLY OUTPUTS =

[[3, 6, 1],

[2, 5, 2],

[1, 2, 1]


I am on python ver 3.7.0 and have tried slipicing instead of copying the string too but the same thing happens. Anyone know why?










share|improve this question























  • What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
    – UltraInstinct
    Nov 12 '18 at 23:17










  • Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
    – Albert gonzal
    Nov 12 '18 at 23:31
















2














So I currently have a function to rotate a matrix counter-clockwise, the list is always a square grid:



def rotate(m: List[List[int]]) -> None:
temp = m.copy()

if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]

m = temp.copy()


I've traced this function repeatedly and to my knowledge it should be working. The problem is that for some reason every change to temp affects the original list. For example,



ORIGINAL =

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

WHAT SHOULD OUTPUT =

[[3, 6, 9],

[2, 5, 8],

[1, 4, 7]]

WHAT ACTUALLY OUTPUTS =

[[3, 6, 1],

[2, 5, 2],

[1, 2, 1]


I am on python ver 3.7.0 and have tried slipicing instead of copying the string too but the same thing happens. Anyone know why?










share|improve this question























  • What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
    – UltraInstinct
    Nov 12 '18 at 23:17










  • Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
    – Albert gonzal
    Nov 12 '18 at 23:31














2












2








2







So I currently have a function to rotate a matrix counter-clockwise, the list is always a square grid:



def rotate(m: List[List[int]]) -> None:
temp = m.copy()

if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]

m = temp.copy()


I've traced this function repeatedly and to my knowledge it should be working. The problem is that for some reason every change to temp affects the original list. For example,



ORIGINAL =

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

WHAT SHOULD OUTPUT =

[[3, 6, 9],

[2, 5, 8],

[1, 4, 7]]

WHAT ACTUALLY OUTPUTS =

[[3, 6, 1],

[2, 5, 2],

[1, 2, 1]


I am on python ver 3.7.0 and have tried slipicing instead of copying the string too but the same thing happens. Anyone know why?










share|improve this question















So I currently have a function to rotate a matrix counter-clockwise, the list is always a square grid:



def rotate(m: List[List[int]]) -> None:
temp = m.copy()

if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]

m = temp.copy()


I've traced this function repeatedly and to my knowledge it should be working. The problem is that for some reason every change to temp affects the original list. For example,



ORIGINAL =

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

WHAT SHOULD OUTPUT =

[[3, 6, 9],

[2, 5, 8],

[1, 4, 7]]

WHAT ACTUALLY OUTPUTS =

[[3, 6, 1],

[2, 5, 2],

[1, 2, 1]


I am on python ver 3.7.0 and have tried slipicing instead of copying the string too but the same thing happens. Anyone know why?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:41









usr2564301

17.7k73270




17.7k73270










asked Nov 12 '18 at 23:14









Albert gonzalAlbert gonzal

132




132











  • What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
    – UltraInstinct
    Nov 12 '18 at 23:17










  • Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
    – Albert gonzal
    Nov 12 '18 at 23:31

















  • What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
    – UltraInstinct
    Nov 12 '18 at 23:17










  • Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
    – Albert gonzal
    Nov 12 '18 at 23:31
















What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
– UltraInstinct
Nov 12 '18 at 23:17




What happens when you replace temp = m.copy() with temp = deepcopy(m) (of course you'll have to import deepcopy)?
– UltraInstinct
Nov 12 '18 at 23:17












Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
– Albert gonzal
Nov 12 '18 at 23:31





Using deepcopy just outputs the original list back again for some reason. I tried using temp = deepcopy(m) and m = temp.copy() first, then used deepcopy on both but they both just output the original list
– Albert gonzal
Nov 12 '18 at 23:31













2 Answers
2






active

oldest

votes


















3














Since every item in the m list is a reference to a sublist, when you make a copy of the m list by calling m.copy(), it is only copying the references of the sublists without creating new copies of the sublists, which is why every change to the temp list is reflected on the original m list.



You should use copy.deepcopy() instead to make copies of the sublists as well:



from copy import deepcopy
def rotate(m):
temp = deepcopy(m)
if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]
return temp


so that:



rotate([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])


returns:



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


Alternatively, you can implement the same matrix rotation by zipping the list of lists and reversing it:



def rotate(m):
return list(zip(*m))[::-1]





share|improve this answer






















  • I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
    – Albert gonzal
    Nov 12 '18 at 23:49











  • Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
    – Albert gonzal
    Nov 12 '18 at 23:53










  • out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
    – Tim Gottgetreu
    Nov 12 '18 at 23:57










  • @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
    – blhsing
    Nov 13 '18 at 0:30






  • 1




    FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
    – Albert gonzal
    Nov 13 '18 at 1:17


















0














import copy

def rotate(m: [[int]]):
temp = copy.deepcopy(m)
if len(m) > 1:
for x in range(len(m)):
for y in range(len(m)):
temp[len(m)-1-y][x] = m[x][y]
return temp


So:



rotate([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])


Output:



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


Example: https://onlinegdb.com/ryGU1jD6X






share|improve this answer




















    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%2f53271464%2frotating-a-matrix-counter-clockwise-changes-the-original-as-well%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









    3














    Since every item in the m list is a reference to a sublist, when you make a copy of the m list by calling m.copy(), it is only copying the references of the sublists without creating new copies of the sublists, which is why every change to the temp list is reflected on the original m list.



    You should use copy.deepcopy() instead to make copies of the sublists as well:



    from copy import deepcopy
    def rotate(m):
    temp = deepcopy(m)
    if len(m) > 1:
    for x in range(len(m)):
    for y in range(len(m)):
    temp[len(m)-1-y][x] = m[x][y]
    return temp


    so that:



    rotate([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ])


    returns:



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


    Alternatively, you can implement the same matrix rotation by zipping the list of lists and reversing it:



    def rotate(m):
    return list(zip(*m))[::-1]





    share|improve this answer






















    • I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
      – Albert gonzal
      Nov 12 '18 at 23:49











    • Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
      – Albert gonzal
      Nov 12 '18 at 23:53










    • out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
      – Tim Gottgetreu
      Nov 12 '18 at 23:57










    • @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
      – blhsing
      Nov 13 '18 at 0:30






    • 1




      FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
      – Albert gonzal
      Nov 13 '18 at 1:17















    3














    Since every item in the m list is a reference to a sublist, when you make a copy of the m list by calling m.copy(), it is only copying the references of the sublists without creating new copies of the sublists, which is why every change to the temp list is reflected on the original m list.



    You should use copy.deepcopy() instead to make copies of the sublists as well:



    from copy import deepcopy
    def rotate(m):
    temp = deepcopy(m)
    if len(m) > 1:
    for x in range(len(m)):
    for y in range(len(m)):
    temp[len(m)-1-y][x] = m[x][y]
    return temp


    so that:



    rotate([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ])


    returns:



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


    Alternatively, you can implement the same matrix rotation by zipping the list of lists and reversing it:



    def rotate(m):
    return list(zip(*m))[::-1]





    share|improve this answer






















    • I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
      – Albert gonzal
      Nov 12 '18 at 23:49











    • Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
      – Albert gonzal
      Nov 12 '18 at 23:53










    • out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
      – Tim Gottgetreu
      Nov 12 '18 at 23:57










    • @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
      – blhsing
      Nov 13 '18 at 0:30






    • 1




      FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
      – Albert gonzal
      Nov 13 '18 at 1:17













    3












    3








    3






    Since every item in the m list is a reference to a sublist, when you make a copy of the m list by calling m.copy(), it is only copying the references of the sublists without creating new copies of the sublists, which is why every change to the temp list is reflected on the original m list.



    You should use copy.deepcopy() instead to make copies of the sublists as well:



    from copy import deepcopy
    def rotate(m):
    temp = deepcopy(m)
    if len(m) > 1:
    for x in range(len(m)):
    for y in range(len(m)):
    temp[len(m)-1-y][x] = m[x][y]
    return temp


    so that:



    rotate([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ])


    returns:



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


    Alternatively, you can implement the same matrix rotation by zipping the list of lists and reversing it:



    def rotate(m):
    return list(zip(*m))[::-1]





    share|improve this answer














    Since every item in the m list is a reference to a sublist, when you make a copy of the m list by calling m.copy(), it is only copying the references of the sublists without creating new copies of the sublists, which is why every change to the temp list is reflected on the original m list.



    You should use copy.deepcopy() instead to make copies of the sublists as well:



    from copy import deepcopy
    def rotate(m):
    temp = deepcopy(m)
    if len(m) > 1:
    for x in range(len(m)):
    for y in range(len(m)):
    temp[len(m)-1-y][x] = m[x][y]
    return temp


    so that:



    rotate([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ])


    returns:



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


    Alternatively, you can implement the same matrix rotation by zipping the list of lists and reversing it:



    def rotate(m):
    return list(zip(*m))[::-1]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 '18 at 23:44









    fuzz

    15.2k17108183




    15.2k17108183










    answered Nov 12 '18 at 23:39









    blhsingblhsing

    28.7k41336




    28.7k41336











    • I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
      – Albert gonzal
      Nov 12 '18 at 23:49











    • Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
      – Albert gonzal
      Nov 12 '18 at 23:53










    • out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
      – Tim Gottgetreu
      Nov 12 '18 at 23:57










    • @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
      – blhsing
      Nov 13 '18 at 0:30






    • 1




      FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
      – Albert gonzal
      Nov 13 '18 at 1:17
















    • I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
      – Albert gonzal
      Nov 12 '18 at 23:49











    • Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
      – Albert gonzal
      Nov 12 '18 at 23:53










    • out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
      – Tim Gottgetreu
      Nov 12 '18 at 23:57










    • @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
      – blhsing
      Nov 13 '18 at 0:30






    • 1




      FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
      – Albert gonzal
      Nov 13 '18 at 1:17















    I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
    – Albert gonzal
    Nov 12 '18 at 23:49





    I don't want to return anything for this function. And using deepcopy for both temp and then m = deepcopy(temp) doesnt change any values of m. I'm trying to rotate m with no return from the function.
    – Albert gonzal
    Nov 12 '18 at 23:49













    Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
    – Albert gonzal
    Nov 12 '18 at 23:53




    Also, I haven't learned deepcopy in my class, so I don't wanna use anything new for this
    – Albert gonzal
    Nov 12 '18 at 23:53












    out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
    – Tim Gottgetreu
    Nov 12 '18 at 23:57




    out of curiosity would temp = m.copy(deep = True) be the same as temp = deepcopy(m)
    – Tim Gottgetreu
    Nov 12 '18 at 23:57












    @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
    – blhsing
    Nov 13 '18 at 0:30




    @Albertgonzal If you don't want the function to return anything you can simply assign temp to m with m = temp in the end instead of return temp.
    – blhsing
    Nov 13 '18 at 0:30




    1




    1




    FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
    – Albert gonzal
    Nov 13 '18 at 1:17




    FINALLY, thank you so much, I changed my code a little so i wouldn't have to use deepcopy but this was the last step i needed. Thanks a lot
    – Albert gonzal
    Nov 13 '18 at 1:17













    0














    import copy

    def rotate(m: [[int]]):
    temp = copy.deepcopy(m)
    if len(m) > 1:
    for x in range(len(m)):
    for y in range(len(m)):
    temp[len(m)-1-y][x] = m[x][y]
    return temp


    So:



    rotate([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ])


    Output:



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


    Example: https://onlinegdb.com/ryGU1jD6X






    share|improve this answer

























      0














      import copy

      def rotate(m: [[int]]):
      temp = copy.deepcopy(m)
      if len(m) > 1:
      for x in range(len(m)):
      for y in range(len(m)):
      temp[len(m)-1-y][x] = m[x][y]
      return temp


      So:



      rotate([
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9]
      ])


      Output:



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


      Example: https://onlinegdb.com/ryGU1jD6X






      share|improve this answer























        0












        0








        0






        import copy

        def rotate(m: [[int]]):
        temp = copy.deepcopy(m)
        if len(m) > 1:
        for x in range(len(m)):
        for y in range(len(m)):
        temp[len(m)-1-y][x] = m[x][y]
        return temp


        So:



        rotate([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
        ])


        Output:



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


        Example: https://onlinegdb.com/ryGU1jD6X






        share|improve this answer












        import copy

        def rotate(m: [[int]]):
        temp = copy.deepcopy(m)
        if len(m) > 1:
        for x in range(len(m)):
        for y in range(len(m)):
        temp[len(m)-1-y][x] = m[x][y]
        return temp


        So:



        rotate([
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
        ])


        Output:



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


        Example: https://onlinegdb.com/ryGU1jD6X







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 0:46









        fuzzfuzz

        15.2k17108183




        15.2k17108183



























            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%2f53271464%2frotating-a-matrix-counter-clockwise-changes-the-original-as-well%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3