Replace elements in an array (python) [duplicate]
This question already has an answer here:
Translate integers in a numpy array to a contiguous range 0…n
2 answers
I have an array:
y=['a','s','d','a','f','d','g']
I want to replace all elements of this array by integers.
I thought that a simple solution would be to do the following:
c = np.unique(y)
and then replace all elements in y with their index in c.
I can possibly do elementwise comparisons like this:
for n, i in enumerate(c):
for m, j in enumerate(y):
if i == j:
y[m] = n
output:
y=[0, 4, 1, 0, 2, 1, 3]
but is there a numpy function to do this in a compact way?
python arrays numpy
marked as duplicate by Divakar
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 6:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Translate integers in a numpy array to a contiguous range 0…n
2 answers
I have an array:
y=['a','s','d','a','f','d','g']
I want to replace all elements of this array by integers.
I thought that a simple solution would be to do the following:
c = np.unique(y)
and then replace all elements in y with their index in c.
I can possibly do elementwise comparisons like this:
for n, i in enumerate(c):
for m, j in enumerate(y):
if i == j:
y[m] = n
output:
y=[0, 4, 1, 0, 2, 1, 3]
but is there a numpy function to do this in a compact way?
python arrays numpy
marked as duplicate by Divakar
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 6:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Translate integers in a numpy array to a contiguous range 0…n
2 answers
I have an array:
y=['a','s','d','a','f','d','g']
I want to replace all elements of this array by integers.
I thought that a simple solution would be to do the following:
c = np.unique(y)
and then replace all elements in y with their index in c.
I can possibly do elementwise comparisons like this:
for n, i in enumerate(c):
for m, j in enumerate(y):
if i == j:
y[m] = n
output:
y=[0, 4, 1, 0, 2, 1, 3]
but is there a numpy function to do this in a compact way?
python arrays numpy
This question already has an answer here:
Translate integers in a numpy array to a contiguous range 0…n
2 answers
I have an array:
y=['a','s','d','a','f','d','g']
I want to replace all elements of this array by integers.
I thought that a simple solution would be to do the following:
c = np.unique(y)
and then replace all elements in y with their index in c.
I can possibly do elementwise comparisons like this:
for n, i in enumerate(c):
for m, j in enumerate(y):
if i == j:
y[m] = n
output:
y=[0, 4, 1, 0, 2, 1, 3]
but is there a numpy function to do this in a compact way?
This question already has an answer here:
Translate integers in a numpy array to a contiguous range 0…n
2 answers
python arrays numpy
python arrays numpy
asked Nov 25 '18 at 6:46
user1340852user1340852
3482315
3482315
marked as duplicate by Divakar
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 6:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Divakar
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 6:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
y = [c.tolist().index(i) for i in y]
Just usereturn_inverse=True
as an argument tonp.unique
.
– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
y = [c.tolist().index(i) for i in y]
Just usereturn_inverse=True
as an argument tonp.unique
.
– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
add a comment |
y = [c.tolist().index(i) for i in y]
Just usereturn_inverse=True
as an argument tonp.unique
.
– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
add a comment |
y = [c.tolist().index(i) for i in y]
y = [c.tolist().index(i) for i in y]
answered Nov 25 '18 at 6:52
Biswadip MandalBiswadip Mandal
1809
1809
Just usereturn_inverse=True
as an argument tonp.unique
.
– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
add a comment |
Just usereturn_inverse=True
as an argument tonp.unique
.
– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
Just use
return_inverse=True
as an argument to np.unique
.– Jan Christoph Terasa
Nov 25 '18 at 8:38
Just use
return_inverse=True
as an argument to np.unique
.– Jan Christoph Terasa
Nov 25 '18 at 8:38
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
Hey @JanChristophTerasa that sounds nice. Thanks :)
– Biswadip Mandal
Nov 25 '18 at 10:47
add a comment |