Replace elements in an array (python) [duplicate]












0
















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?










share|improve this question













marked as duplicate by Divakar arrays
Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

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.























    0
















    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?










    share|improve this question













    marked as duplicate by Divakar arrays
    Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

    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.





















      0












      0








      0









      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?










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 6:46









      user1340852user1340852

      3482315




      3482315




      marked as duplicate by Divakar arrays
      Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

      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 arrays
      Users with the  arrays badge can single-handedly close arrays questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          y = [c.tolist().index(i) for i in y]





          share|improve this answer
























          • 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


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          y = [c.tolist().index(i) for i in y]





          share|improve this answer
























          • 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
















          0














          y = [c.tolist().index(i) for i in y]





          share|improve this answer
























          • 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














          0












          0








          0







          y = [c.tolist().index(i) for i in y]





          share|improve this answer













          y = [c.tolist().index(i) for i in y]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 6:52









          Biswadip MandalBiswadip Mandal

          1809




          1809













          • 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



















          • 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

















          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





          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga