How do I make append work as intended with arrays?How to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?What is the difference between Python's list methods append and extend?How to print a date in a regular format?How can I make a time delay in Python?Convert bytes to a string?How to make a chain of function decorators?How to make a flat list out of list of lists“Least Astonishment” and the Mutable Default ArgumentHow do I list all files of a directory?
When is the phrase "j'ai bon" used?
How valuable is a categorical feature that has a predominant category over all other ones?
How did the European Union reach the figure of 3% as a maximum allowed deficit?
How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
Bash function: Execute $@ command with each argument in sequence executed separately
writing a function between sets vertically
A medieval book with a redhead girl as a main character who allies with vampires and werewolves against scientific opposition
Do battery electrons only move if there is a positive terminal at the end of the wire?
How can I detect if I'm in a subshell?
How can I maintain game balance while allowing my player to craft genuinely useful items?
First occurrence in the Sixers sequence
How "fast" do astronomical events occur?
How to ask if I can mow my neighbor's lawn
Fantasy game inventory — Ch. 5 Automate the Boring Stuff
Build a scale without computer
Got a new frameset, don't know why I need this split ring collar?
How can I ping multiple IP addresses at the same time?
I just entered the USA without passport control at Atlanta airport
Co-worker is now managing my team. Does this mean that I'm being demoted?
How can I prevent a user from copying files on another hard drive?
How can the US president give an order to a civilian?
Fill the maze with a wall-following Snake until it gets stuck
Is it a bad idea to have a pen name with only an initial for a surname?
How do I make append work as intended with arrays?
How to merge two dictionaries in a single expression?How do I check whether a file exists without exceptions?What is the difference between Python's list methods append and extend?How to print a date in a regular format?How can I make a time delay in Python?Convert bytes to a string?How to make a chain of function decorators?How to make a flat list out of list of lists“Least Astonishment” and the Mutable Default ArgumentHow do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm having problem with this code. I think I'm doing something wrong.
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array)
print(arrays[0])
I was expecting to get:[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
But I'm getting:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
That is the last array I appended to arrays, and not the first one. Why is that happening and more important what can I do to get the desired output?
python numpy
|
show 3 more comments
I'm having problem with this code. I think I'm doing something wrong.
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array)
print(arrays[0])
I was expecting to get:[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
But I'm getting:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
That is the last array I appended to arrays, and not the first one. Why is that happening and more important what can I do to get the desired output?
python numpy
3
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
1
Also why not usingnp.eye
?
– Julien
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
1
When you modify an object - list, dictionary,ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.
– hpaulj
Mar 25 at 6:59
|
show 3 more comments
I'm having problem with this code. I think I'm doing something wrong.
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array)
print(arrays[0])
I was expecting to get:[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
But I'm getting:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
That is the last array I appended to arrays, and not the first one. Why is that happening and more important what can I do to get the desired output?
python numpy
I'm having problem with this code. I think I'm doing something wrong.
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array)
print(arrays[0])
I was expecting to get:[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
But I'm getting:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
That is the last array I appended to arrays, and not the first one. Why is that happening and more important what can I do to get the desired output?
python numpy
python numpy
edited Mar 25 at 7:13
hpaulj
122k792167
122k792167
asked Mar 25 at 4:54
Alejandro RuizAlejandro Ruiz
297
297
3
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
1
Also why not usingnp.eye
?
– Julien
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
1
When you modify an object - list, dictionary,ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.
– hpaulj
Mar 25 at 6:59
|
show 3 more comments
3
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
1
Also why not usingnp.eye
?
– Julien
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
1
When you modify an object - list, dictionary,ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.
– hpaulj
Mar 25 at 6:59
3
3
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
1
1
Also why not using
np.eye
?– Julien
Mar 25 at 4:58
Also why not using
np.eye
?– Julien
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
1
1
When you modify an object - list, dictionary,
ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.– hpaulj
Mar 25 at 6:59
When you modify an object - list, dictionary,
ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.– hpaulj
Mar 25 at 6:59
|
show 3 more comments
5 Answers
5
active
oldest
votes
I think you are expecting:
arrays.append(array)
to add a COPY of your main array to the arrays list. But that's not what you're doing. You're pushing another reference to the same array each time you do:
arrays.append(array)
so at the end of your loop, you have the list arrays with 10 references to the same original array you created. By then, you've set every value of that ONE ARRAY to 1. So you get that the first value in arrays contains an array with every value set to 1 because every array in arrays is that same array.
If you actually copy a new array each time into arrays, I bet you'll get what you expected. To do that, change that line to:
arrays.append(array.copy())
Here's a complete version of your program with this fix. I changed it also to print all 10 of the arrays in arrays:
def main():
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array.copy())
for array in arrays:
print(array)
Result:
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.
– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
add a comment |
just add this change:
arrays.append(np.array(array))
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
add a comment |
The actual way to do this in numpy
is with np.tri()
:
np.tri(10)
Out[]:
array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 1., 1., 0., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 1., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 0.],
[ 1., 1., 1., ..., 1., 1., 1.]])
add a comment |
Maybe you are looking for this , just added if condition in your code
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
if i==0:
array[i] = 1
arrays.append(array)
print(arrays[0])
out: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
add a comment |
You can use array.copy()
a method defined on numpy arrays as @Steve has suggested.
As it has been already used in one of the answer (@Steve's answer) to this problem so I choose another approach i.e.
deepcopy() function
to obtain the result.
import numpy as np
from copy import deepcopy
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(deepcopy(array))
print(arrays)
# [array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])]
print(arrays[0])
# [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arrays[-1])
# [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Thank you for your comment @tripleee . I have updated my answer. I knowarray.copy()
is good choice but it is already a part of 1 answer so useddeepcopy()
to get the result.
– hygull
Mar 25 at 11:05
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%2f55331441%2fhow-do-i-make-append-work-as-intended-with-arrays%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are expecting:
arrays.append(array)
to add a COPY of your main array to the arrays list. But that's not what you're doing. You're pushing another reference to the same array each time you do:
arrays.append(array)
so at the end of your loop, you have the list arrays with 10 references to the same original array you created. By then, you've set every value of that ONE ARRAY to 1. So you get that the first value in arrays contains an array with every value set to 1 because every array in arrays is that same array.
If you actually copy a new array each time into arrays, I bet you'll get what you expected. To do that, change that line to:
arrays.append(array.copy())
Here's a complete version of your program with this fix. I changed it also to print all 10 of the arrays in arrays:
def main():
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array.copy())
for array in arrays:
print(array)
Result:
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.
– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
add a comment |
I think you are expecting:
arrays.append(array)
to add a COPY of your main array to the arrays list. But that's not what you're doing. You're pushing another reference to the same array each time you do:
arrays.append(array)
so at the end of your loop, you have the list arrays with 10 references to the same original array you created. By then, you've set every value of that ONE ARRAY to 1. So you get that the first value in arrays contains an array with every value set to 1 because every array in arrays is that same array.
If you actually copy a new array each time into arrays, I bet you'll get what you expected. To do that, change that line to:
arrays.append(array.copy())
Here's a complete version of your program with this fix. I changed it also to print all 10 of the arrays in arrays:
def main():
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array.copy())
for array in arrays:
print(array)
Result:
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.
– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
add a comment |
I think you are expecting:
arrays.append(array)
to add a COPY of your main array to the arrays list. But that's not what you're doing. You're pushing another reference to the same array each time you do:
arrays.append(array)
so at the end of your loop, you have the list arrays with 10 references to the same original array you created. By then, you've set every value of that ONE ARRAY to 1. So you get that the first value in arrays contains an array with every value set to 1 because every array in arrays is that same array.
If you actually copy a new array each time into arrays, I bet you'll get what you expected. To do that, change that line to:
arrays.append(array.copy())
Here's a complete version of your program with this fix. I changed it also to print all 10 of the arrays in arrays:
def main():
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array.copy())
for array in arrays:
print(array)
Result:
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
I think you are expecting:
arrays.append(array)
to add a COPY of your main array to the arrays list. But that's not what you're doing. You're pushing another reference to the same array each time you do:
arrays.append(array)
so at the end of your loop, you have the list arrays with 10 references to the same original array you created. By then, you've set every value of that ONE ARRAY to 1. So you get that the first value in arrays contains an array with every value set to 1 because every array in arrays is that same array.
If you actually copy a new array each time into arrays, I bet you'll get what you expected. To do that, change that line to:
arrays.append(array.copy())
Here's a complete version of your program with this fix. I changed it also to print all 10 of the arrays in arrays:
def main():
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(array.copy())
for array in arrays:
print(array)
Result:
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
edited Mar 25 at 5:51
answered Mar 25 at 5:10
SteveSteve
4,1801728
4,1801728
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.
– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
add a comment |
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.
– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.– user2357112
Mar 25 at 5:33
array
is not a list; it's a NumPy array. They look similar, but the types are very different and should not be mixed up.– user2357112
Mar 25 at 5:33
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
Doh! Thanks for pointing that out. Then I showed how to fix it incorrectly, lol! I don't know numpy. I showed the problem, and the idea of how to fix it. If the guy really needs a numpy array, I hope he'd be able to fix this himself. Still...you're right...I'll amend my answer. - guess my output looking different when printed should have tipped me off. I don't act all that bright sometimes. Thanks again!
– Steve
Mar 25 at 5:44
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
@AlejandroRuiz If this solved your problem, please consider accepting it. See also help
– tripleee
Mar 25 at 11:14
add a comment |
just add this change:
arrays.append(np.array(array))
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
add a comment |
just add this change:
arrays.append(np.array(array))
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
add a comment |
just add this change:
arrays.append(np.array(array))
just add this change:
arrays.append(np.array(array))
edited Mar 25 at 6:00
answered Mar 25 at 5:15
Ali HallajiAli Hallaji
9581119
9581119
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
add a comment |
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
You made the same mistake I did originally in my answer. array isn't a list...it's a numpy array. So here, you're changing the type of the container, not just copying it. Like I said, I made the same mistake originally. It took @user2357112 to point out the error of my ways.
– Steve
Mar 25 at 5:58
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
you are right.I have change it to np.array().
– Ali Hallaji
Mar 25 at 6:09
add a comment |
The actual way to do this in numpy
is with np.tri()
:
np.tri(10)
Out[]:
array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 1., 1., 0., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 1., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 0.],
[ 1., 1., 1., ..., 1., 1., 1.]])
add a comment |
The actual way to do this in numpy
is with np.tri()
:
np.tri(10)
Out[]:
array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 1., 1., 0., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 1., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 0.],
[ 1., 1., 1., ..., 1., 1., 1.]])
add a comment |
The actual way to do this in numpy
is with np.tri()
:
np.tri(10)
Out[]:
array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 1., 1., 0., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 1., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 0.],
[ 1., 1., 1., ..., 1., 1., 1.]])
The actual way to do this in numpy
is with np.tri()
:
np.tri(10)
Out[]:
array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 1., 1., 0., ..., 0., 0., 0.],
[ 1., 1., 1., ..., 0., 0., 0.],
...,
[ 1., 1., 1., ..., 1., 0., 0.],
[ 1., 1., 1., ..., 1., 1., 0.],
[ 1., 1., 1., ..., 1., 1., 1.]])
edited Mar 25 at 10:51
answered Mar 25 at 10:46
Daniel FDaniel F
7,5621432
7,5621432
add a comment |
add a comment |
Maybe you are looking for this , just added if condition in your code
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
if i==0:
array[i] = 1
arrays.append(array)
print(arrays[0])
out: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
add a comment |
Maybe you are looking for this , just added if condition in your code
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
if i==0:
array[i] = 1
arrays.append(array)
print(arrays[0])
out: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
add a comment |
Maybe you are looking for this , just added if condition in your code
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
if i==0:
array[i] = 1
arrays.append(array)
print(arrays[0])
out: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Maybe you are looking for this , just added if condition in your code
import numpy as np
array = np.zeros(10)
arrays = []
for i in range(len(array)):
if i==0:
array[i] = 1
arrays.append(array)
print(arrays[0])
out: [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
answered Mar 25 at 5:12
NickelNickel
1278
1278
add a comment |
add a comment |
You can use array.copy()
a method defined on numpy arrays as @Steve has suggested.
As it has been already used in one of the answer (@Steve's answer) to this problem so I choose another approach i.e.
deepcopy() function
to obtain the result.
import numpy as np
from copy import deepcopy
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(deepcopy(array))
print(arrays)
# [array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])]
print(arrays[0])
# [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arrays[-1])
# [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Thank you for your comment @tripleee . I have updated my answer. I knowarray.copy()
is good choice but it is already a part of 1 answer so useddeepcopy()
to get the result.
– hygull
Mar 25 at 11:05
add a comment |
You can use array.copy()
a method defined on numpy arrays as @Steve has suggested.
As it has been already used in one of the answer (@Steve's answer) to this problem so I choose another approach i.e.
deepcopy() function
to obtain the result.
import numpy as np
from copy import deepcopy
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(deepcopy(array))
print(arrays)
# [array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])]
print(arrays[0])
# [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arrays[-1])
# [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
Thank you for your comment @tripleee . I have updated my answer. I knowarray.copy()
is good choice but it is already a part of 1 answer so useddeepcopy()
to get the result.
– hygull
Mar 25 at 11:05
add a comment |
You can use array.copy()
a method defined on numpy arrays as @Steve has suggested.
As it has been already used in one of the answer (@Steve's answer) to this problem so I choose another approach i.e.
deepcopy() function
to obtain the result.
import numpy as np
from copy import deepcopy
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(deepcopy(array))
print(arrays)
# [array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])]
print(arrays[0])
# [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arrays[-1])
# [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
You can use array.copy()
a method defined on numpy arrays as @Steve has suggested.
As it has been already used in one of the answer (@Steve's answer) to this problem so I choose another approach i.e.
deepcopy() function
to obtain the result.
import numpy as np
from copy import deepcopy
array = np.zeros(10)
arrays = []
for i in range(len(array)):
array[i] = 1
arrays.append(deepcopy(array))
print(arrays)
# [array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 0., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 0., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 0., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 0., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 0., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 0., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]), array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])]
print(arrays[0])
# [1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
print(arrays[-1])
# [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
edited Mar 25 at 11:11
answered Mar 25 at 5:05
hygullhygull
4,57021632
4,57021632
Thank you for your comment @tripleee . I have updated my answer. I knowarray.copy()
is good choice but it is already a part of 1 answer so useddeepcopy()
to get the result.
– hygull
Mar 25 at 11:05
add a comment |
Thank you for your comment @tripleee . I have updated my answer. I knowarray.copy()
is good choice but it is already a part of 1 answer so useddeepcopy()
to get the result.
– hygull
Mar 25 at 11:05
Thank you for your comment @tripleee . I have updated my answer. I know
array.copy()
is good choice but it is already a part of 1 answer so used deepcopy()
to get the result.– hygull
Mar 25 at 11:05
Thank you for your comment @tripleee . I have updated my answer. I know
array.copy()
is good choice but it is already a part of 1 answer so used deepcopy()
to get the result.– hygull
Mar 25 at 11:05
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.
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%2f55331441%2fhow-do-i-make-append-work-as-intended-with-arrays%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
3
Hint: how many different array are there in your code? How many arrays do you create?
– Jörg W Mittag
Mar 25 at 4:56
1
Also why not using
np.eye
?– Julien
Mar 25 at 4:58
convert array to nympy array
– prashant rana
Mar 25 at 4:58
I used a simple example to show what's happening, in reality I'm using array of matrices with values I get from data, but the problem is the same.
– Alejandro Ruiz
Mar 25 at 5:02
1
When you modify an object - list, dictionary,
ndarray
, and append it to a list, you need to append a copy, not the object that you keep modifying. Otherwise, all elements of the list will end up looking the same - because they are the same object. List append does not automatically save a copy; you have to do that yourself.– hpaulj
Mar 25 at 6:59