Replace multiple elements in numpy array with 1

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite
3












In a given numpy array X:



X = array([1,2,3,4,5,6,7,8,9,10])


I would like to replace indices (2, 3) and (7, 8) with a single element -1 respectively, like:



X = array([1,2,-1,5,6,7,-1,10])


In other words, I replaced values at indices (2, 3) and (7,8) of the original array with a singular value.



Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.



Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.










share|improve this question



















  • 2




    you don't have to be rude, especially when you are referring to a manual!
    – khan
    2 hours ago










  • Possible duplicate of Replace an element in a numpy array at specific index
    – awiebe
    1 hour ago






  • 1




    You should google then, if you have a different meaning in your mind.
    – khan
    1 hour ago






  • 1




    @awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
    – khan
    1 hour ago






  • 1




    Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
    – awiebe
    1 hour ago














up vote
9
down vote

favorite
3












In a given numpy array X:



X = array([1,2,3,4,5,6,7,8,9,10])


I would like to replace indices (2, 3) and (7, 8) with a single element -1 respectively, like:



X = array([1,2,-1,5,6,7,-1,10])


In other words, I replaced values at indices (2, 3) and (7,8) of the original array with a singular value.



Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.



Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.










share|improve this question



















  • 2




    you don't have to be rude, especially when you are referring to a manual!
    – khan
    2 hours ago










  • Possible duplicate of Replace an element in a numpy array at specific index
    – awiebe
    1 hour ago






  • 1




    You should google then, if you have a different meaning in your mind.
    – khan
    1 hour ago






  • 1




    @awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
    – khan
    1 hour ago






  • 1




    Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
    – awiebe
    1 hour ago












up vote
9
down vote

favorite
3









up vote
9
down vote

favorite
3






3





In a given numpy array X:



X = array([1,2,3,4,5,6,7,8,9,10])


I would like to replace indices (2, 3) and (7, 8) with a single element -1 respectively, like:



X = array([1,2,-1,5,6,7,-1,10])


In other words, I replaced values at indices (2, 3) and (7,8) of the original array with a singular value.



Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.



Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.










share|improve this question















In a given numpy array X:



X = array([1,2,3,4,5,6,7,8,9,10])


I would like to replace indices (2, 3) and (7, 8) with a single element -1 respectively, like:



X = array([1,2,-1,5,6,7,-1,10])


In other words, I replaced values at indices (2, 3) and (7,8) of the original array with a singular value.



Question is: Is there a numpy-ish way (i.e. without for loops and usage of python lists) around it? Thanks.



Note: This is NOT equivalent of replacing a single element in-place with another. Its about replacing multiple values with a "singular" value. Thanks.







python arrays numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 45 mins ago









user3483203

27.4k72350




27.4k72350










asked 2 hours ago









khan

1,66583049




1,66583049







  • 2




    you don't have to be rude, especially when you are referring to a manual!
    – khan
    2 hours ago










  • Possible duplicate of Replace an element in a numpy array at specific index
    – awiebe
    1 hour ago






  • 1




    You should google then, if you have a different meaning in your mind.
    – khan
    1 hour ago






  • 1




    @awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
    – khan
    1 hour ago






  • 1




    Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
    – awiebe
    1 hour ago












  • 2




    you don't have to be rude, especially when you are referring to a manual!
    – khan
    2 hours ago










  • Possible duplicate of Replace an element in a numpy array at specific index
    – awiebe
    1 hour ago






  • 1




    You should google then, if you have a different meaning in your mind.
    – khan
    1 hour ago






  • 1




    @awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
    – khan
    1 hour ago






  • 1




    Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
    – awiebe
    1 hour ago







2




2




you don't have to be rude, especially when you are referring to a manual!
– khan
2 hours ago




you don't have to be rude, especially when you are referring to a manual!
– khan
2 hours ago












Possible duplicate of Replace an element in a numpy array at specific index
– awiebe
1 hour ago




Possible duplicate of Replace an element in a numpy array at specific index
– awiebe
1 hour ago




1




1




You should google then, if you have a different meaning in your mind.
– khan
1 hour ago




You should google then, if you have a different meaning in your mind.
– khan
1 hour ago




1




1




@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
– khan
1 hour ago




@awiebe Not sure what this means ... the question is looking for a numpy-ish way of replacing two entities in an array with a single entity (demo-ed by the examples too in which you can clearly see the size of array reduced by 2 elements).
– khan
1 hour ago




1




1




Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
– awiebe
1 hour ago




Ah, I see the question was resizing the array, not replacing the value. That makes more sense then how I read it.
– awiebe
1 hour ago












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










Try np.put:



np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`


So basically use put to do the values for the indexes, then drop the zero-values.



Or as @khan says, can do something that's out of range:



np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])


All Output:



[ 1 2 -1 5 6 7 -1 10]





share|improve this answer


















  • 2




    what if the list contains 0 initially?
    – Ashutosh Chapagain
    1 hour ago






  • 2




    @AshutoshChapagain Oh yeah smart..., i'll edit
    – U9-Forward
    1 hour ago






  • 1




    This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
    – khan
    1 hour ago






  • 1




    Ohh you already edited. Great answer. :-)
    – khan
    1 hour ago






  • 1




    A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
    – khan
    1 hour ago

















up vote
3
down vote













I'm not sure if this can be done in one step, but here's a way using np.delete:



import numpy as np
from operator import itemgetter

X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]


X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]


First we replace the first element of each pair with -1. Then we delete the remaining elements.






share|improve this answer






















  • Amm.. this is not fully a numpy-ish solution tho...
    – U9-Forward
    1 hour ago

















up vote
3
down vote













A solution using numpy.delete, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices



Setup



a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])



a[idx] = -1
np.delete(a, idx[:, 1:])




array([ 1, 2, -1, 5, 6, 7, -1, 10])





share|improve this answer






















  • Definitely seems like a cleaner and more efficient way.
    – BernardL
    1 hour ago










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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
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%2f53000462%2freplace-multiple-elements-in-numpy-array-with-1%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Try np.put:



np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`


So basically use put to do the values for the indexes, then drop the zero-values.



Or as @khan says, can do something that's out of range:



np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])


All Output:



[ 1 2 -1 5 6 7 -1 10]





share|improve this answer


















  • 2




    what if the list contains 0 initially?
    – Ashutosh Chapagain
    1 hour ago






  • 2




    @AshutoshChapagain Oh yeah smart..., i'll edit
    – U9-Forward
    1 hour ago






  • 1




    This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
    – khan
    1 hour ago






  • 1




    Ohh you already edited. Great answer. :-)
    – khan
    1 hour ago






  • 1




    A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
    – khan
    1 hour ago














up vote
0
down vote



accepted










Try np.put:



np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`


So basically use put to do the values for the indexes, then drop the zero-values.



Or as @khan says, can do something that's out of range:



np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])


All Output:



[ 1 2 -1 5 6 7 -1 10]





share|improve this answer


















  • 2




    what if the list contains 0 initially?
    – Ashutosh Chapagain
    1 hour ago






  • 2




    @AshutoshChapagain Oh yeah smart..., i'll edit
    – U9-Forward
    1 hour ago






  • 1




    This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
    – khan
    1 hour ago






  • 1




    Ohh you already edited. Great answer. :-)
    – khan
    1 hour ago






  • 1




    A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
    – khan
    1 hour ago












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Try np.put:



np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`


So basically use put to do the values for the indexes, then drop the zero-values.



Or as @khan says, can do something that's out of range:



np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])


All Output:



[ 1 2 -1 5 6 7 -1 10]





share|improve this answer














Try np.put:



np.put(X, [2,3,7,8], [-1,0]) # `0` can be changed to anything that's not in the array
print(X[X!=0]) # whatever You put as an number in `put`


So basically use put to do the values for the indexes, then drop the zero-values.



Or as @khan says, can do something that's out of range:



np.put(X, [2,3,7,8], [-1,np.max(X)+1])
print(X[X!=X.max()])


All Output:



[ 1 2 -1 5 6 7 -1 10]






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









U9-Forward

7,8342731




7,8342731







  • 2




    what if the list contains 0 initially?
    – Ashutosh Chapagain
    1 hour ago






  • 2




    @AshutoshChapagain Oh yeah smart..., i'll edit
    – U9-Forward
    1 hour ago






  • 1




    This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
    – khan
    1 hour ago






  • 1




    Ohh you already edited. Great answer. :-)
    – khan
    1 hour ago






  • 1




    A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
    – khan
    1 hour ago












  • 2




    what if the list contains 0 initially?
    – Ashutosh Chapagain
    1 hour ago






  • 2




    @AshutoshChapagain Oh yeah smart..., i'll edit
    – U9-Forward
    1 hour ago






  • 1




    This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
    – khan
    1 hour ago






  • 1




    Ohh you already edited. Great answer. :-)
    – khan
    1 hour ago






  • 1




    A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
    – khan
    1 hour ago







2




2




what if the list contains 0 initially?
– Ashutosh Chapagain
1 hour ago




what if the list contains 0 initially?
– Ashutosh Chapagain
1 hour ago




2




2




@AshutoshChapagain Oh yeah smart..., i'll edit
– U9-Forward
1 hour ago




@AshutoshChapagain Oh yeah smart..., i'll edit
– U9-Forward
1 hour ago




1




1




This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
– khan
1 hour ago




This zero can actually be anything out of the range of the array..that will help it. So, X=numpy.arange(1,11); numpy.put(X, [2,3,7,8], [-1,numpy.max(X)+1]); X[X!=X.max()]..
– khan
1 hour ago




1




1




Ohh you already edited. Great answer. :-)
– khan
1 hour ago




Ohh you already edited. Great answer. :-)
– khan
1 hour ago




1




1




A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
– khan
1 hour ago




A food for thought: What if we like to replace indices (2,3,7,8) with -1 and indices (9,10) with -2..
– khan
1 hour ago












up vote
3
down vote













I'm not sure if this can be done in one step, but here's a way using np.delete:



import numpy as np
from operator import itemgetter

X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]


X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]


First we replace the first element of each pair with -1. Then we delete the remaining elements.






share|improve this answer






















  • Amm.. this is not fully a numpy-ish solution tho...
    – U9-Forward
    1 hour ago














up vote
3
down vote













I'm not sure if this can be done in one step, but here's a way using np.delete:



import numpy as np
from operator import itemgetter

X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]


X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]


First we replace the first element of each pair with -1. Then we delete the remaining elements.






share|improve this answer






















  • Amm.. this is not fully a numpy-ish solution tho...
    – U9-Forward
    1 hour ago












up vote
3
down vote










up vote
3
down vote









I'm not sure if this can be done in one step, but here's a way using np.delete:



import numpy as np
from operator import itemgetter

X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]


X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]


First we replace the first element of each pair with -1. Then we delete the remaining elements.






share|improve this answer














I'm not sure if this can be done in one step, but here's a way using np.delete:



import numpy as np
from operator import itemgetter

X = np.array(range(1,11))
to_replace = [[2,3], [7,8]]


X[list(map(itemgetter(0), to_replace))] = -1
X = np.delete(X, list(map(lambda x: x[1:], to_replace)))
print(X)
#[ 1 2 -1 5 6 7 -1 10]


First we replace the first element of each pair with -1. Then we delete the remaining elements.







share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









pault

12.6k31541




12.6k31541











  • Amm.. this is not fully a numpy-ish solution tho...
    – U9-Forward
    1 hour ago
















  • Amm.. this is not fully a numpy-ish solution tho...
    – U9-Forward
    1 hour ago















Amm.. this is not fully a numpy-ish solution tho...
– U9-Forward
1 hour ago




Amm.. this is not fully a numpy-ish solution tho...
– U9-Forward
1 hour ago










up vote
3
down vote













A solution using numpy.delete, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices



Setup



a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])



a[idx] = -1
np.delete(a, idx[:, 1:])




array([ 1, 2, -1, 5, 6, 7, -1, 10])





share|improve this answer






















  • Definitely seems like a cleaner and more efficient way.
    – BernardL
    1 hour ago














up vote
3
down vote













A solution using numpy.delete, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices



Setup



a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])



a[idx] = -1
np.delete(a, idx[:, 1:])




array([ 1, 2, -1, 5, 6, 7, -1, 10])





share|improve this answer






















  • Definitely seems like a cleaner and more efficient way.
    – BernardL
    1 hour ago












up vote
3
down vote










up vote
3
down vote









A solution using numpy.delete, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices



Setup



a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])



a[idx] = -1
np.delete(a, idx[:, 1:])




array([ 1, 2, -1, 5, 6, 7, -1, 10])





share|improve this answer














A solution using numpy.delete, similar to @pault, but more efficient as it uses pure numpy indexing. However, because of this efficient indexing, it means that you cannot pass jagged arrays as indices



Setup



a = np.array([1,2,3,4,5,6,7,8,9,10])
idx = np.stack([[2, 3], [7, 8]])



a[idx] = -1
np.delete(a, idx[:, 1:])




array([ 1, 2, -1, 5, 6, 7, -1, 10])






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









user3483203

27.4k72350




27.4k72350











  • Definitely seems like a cleaner and more efficient way.
    – BernardL
    1 hour ago
















  • Definitely seems like a cleaner and more efficient way.
    – BernardL
    1 hour ago















Definitely seems like a cleaner and more efficient way.
– BernardL
1 hour ago




Definitely seems like a cleaner and more efficient way.
– BernardL
1 hour ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53000462%2freplace-multiple-elements-in-numpy-array-with-1%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?