Rotating a matrix counter-clockwise changes the original as well
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
add a comment |
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
What happens when you replacetemp = m.copy()
withtemp = deepcopy(m)
(of course you'll have to importdeepcopy
)?
– UltraInstinct
Nov 12 '18 at 23:17
Using deepcopy just outputs the original list back again for some reason. I tried usingtemp = deepcopy(m)
andm = temp.copy()
first, then useddeepcopy
on both but they both just output the original list
– Albert gonzal
Nov 12 '18 at 23:31
add a comment |
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
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
python python-3.x
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 replacetemp = m.copy()
withtemp = deepcopy(m)
(of course you'll have to importdeepcopy
)?
– UltraInstinct
Nov 12 '18 at 23:17
Using deepcopy just outputs the original list back again for some reason. I tried usingtemp = deepcopy(m)
andm = temp.copy()
first, then useddeepcopy
on both but they both just output the original list
– Albert gonzal
Nov 12 '18 at 23:31
add a comment |
What happens when you replacetemp = m.copy()
withtemp = deepcopy(m)
(of course you'll have to importdeepcopy
)?
– UltraInstinct
Nov 12 '18 at 23:17
Using deepcopy just outputs the original list back again for some reason. I tried usingtemp = deepcopy(m)
andm = temp.copy()
first, then useddeepcopy
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
add a comment |
2 Answers
2
active
oldest
votes
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]
I don't want to return anything for this function. And using deepcopy for both temp and thenm = 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 learneddeepcopy
in my class, so I don't wanna use anything new for this
– Albert gonzal
Nov 12 '18 at 23:53
out of curiosity wouldtemp = m.copy(deep = True)
be the same astemp = deepcopy(m)
– Tim Gottgetreu
Nov 12 '18 at 23:57
@Albertgonzal If you don't want the function to return anything you can simply assigntemp
tom
withm = temp
in the end instead ofreturn 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 usedeepcopy
but this was the last step i needed. Thanks a lot
– Albert gonzal
Nov 13 '18 at 1:17
|
show 2 more comments
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]
I don't want to return anything for this function. And using deepcopy for both temp and thenm = 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 learneddeepcopy
in my class, so I don't wanna use anything new for this
– Albert gonzal
Nov 12 '18 at 23:53
out of curiosity wouldtemp = m.copy(deep = True)
be the same astemp = deepcopy(m)
– Tim Gottgetreu
Nov 12 '18 at 23:57
@Albertgonzal If you don't want the function to return anything you can simply assigntemp
tom
withm = temp
in the end instead ofreturn 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 usedeepcopy
but this was the last step i needed. Thanks a lot
– Albert gonzal
Nov 13 '18 at 1:17
|
show 2 more comments
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]
I don't want to return anything for this function. And using deepcopy for both temp and thenm = 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 learneddeepcopy
in my class, so I don't wanna use anything new for this
– Albert gonzal
Nov 12 '18 at 23:53
out of curiosity wouldtemp = m.copy(deep = True)
be the same astemp = deepcopy(m)
– Tim Gottgetreu
Nov 12 '18 at 23:57
@Albertgonzal If you don't want the function to return anything you can simply assigntemp
tom
withm = temp
in the end instead ofreturn 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 usedeepcopy
but this was the last step i needed. Thanks a lot
– Albert gonzal
Nov 13 '18 at 1:17
|
show 2 more comments
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]
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]
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 thenm = 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 learneddeepcopy
in my class, so I don't wanna use anything new for this
– Albert gonzal
Nov 12 '18 at 23:53
out of curiosity wouldtemp = m.copy(deep = True)
be the same astemp = deepcopy(m)
– Tim Gottgetreu
Nov 12 '18 at 23:57
@Albertgonzal If you don't want the function to return anything you can simply assigntemp
tom
withm = temp
in the end instead ofreturn 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 usedeepcopy
but this was the last step i needed. Thanks a lot
– Albert gonzal
Nov 13 '18 at 1:17
|
show 2 more comments
I don't want to return anything for this function. And using deepcopy for both temp and thenm = 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 learneddeepcopy
in my class, so I don't wanna use anything new for this
– Albert gonzal
Nov 12 '18 at 23:53
out of curiosity wouldtemp = m.copy(deep = True)
be the same astemp = deepcopy(m)
– Tim Gottgetreu
Nov 12 '18 at 23:57
@Albertgonzal If you don't want the function to return anything you can simply assigntemp
tom
withm = temp
in the end instead ofreturn 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 usedeepcopy
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
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
answered Nov 13 '18 at 0:46
fuzzfuzz
15.2k17108183
15.2k17108183
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What happens when you replace
temp = m.copy()
withtemp = deepcopy(m)
(of course you'll have to importdeepcopy
)?– 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)
andm = temp.copy()
first, then useddeepcopy
on both but they both just output the original list– Albert gonzal
Nov 12 '18 at 23:31