Help us understand the problem. What is going on with this article?

[python]numpy形式でリストの値を対応した値に変換する。

[python]numpy形式でリストの値を対応した値に変換する。

行いたいこと

import numpy as np 
array1 = np.array([ 6,  6,  6,  6, 27, 27, 27, 27, 37, 37, 37, 37,  9,  9,  9,  9, 10,
       10, 10, 10, 19, 19, 19, 19, 13, 13, 13, 13, 35, 35, 35, 35, 14, 14,
       14, 14,  1,  1,  1,  1, 11, 11, 11, 11, 32, 32, 32, 32, 33, 33, 33,
       33, 15, 15, 15, 15, 16, 16, 16, 16, 18, 18, 18, 18, 28, 28, 28, 28,
        5,  5,  5,  5, 39, 39, 39, 39, 31, 31, 31, 31,  0,  0,  0,  0,  8,
        8,  8,  8, 22, 22, 22, 22,  7,  7,  7,  7,  2,  2,  2,  2, 25, 25,
       25, 25, 21, 21, 21, 21, 29, 29, 29, 29, 36, 36, 36, 36, 30, 30, 30,
       30,  3,  3,  3,  3, 38, 38, 38, 38,  4,  4,  4,  4, 23, 23, 23, 23,
       17, 17, 17, 17, 24, 24, 24, 24, 12, 12, 12, 12, 20, 20, 20, 20, 34,
       34, 34, 34, 26, 26, 26, 26])

array2 = np.array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39])

array3 = np.array([ 4, 38, 33, 15, 27,  2, 17, 23,  7, 31, 18, 19, 16, 37,  9, 32,  0,
       22, 24, 28, 25,  1,  5, 20,  6, 36, 26, 35, 39, 10, 12, 30, 34, 21,
       14, 29, 11, 13,  8,  3])

array1の値をarray2と対応するarray3の値に変換したい。

つまり、以下のような配列を作成したい。

array([17, 17, 17, 17, 35, 35, 35, 35, 13, 13, 13, 13, 31, 31, 31, 31, 18,
       18, 18, 18, 28, 28, 28, 28, 37, 37, 37, 37, 29, 29, 29, 29,  9,  9,
        9,  9, 38, 38, 38, 38, 19, 19, 19, 19, 34, 34, 34, 34, 21, 21, 21,
       21, 32, 32, 32, 32,  0,  0,  0,  0, 24, 24, 24, 24, 39, 39, 39, 39,
        2,  2,  2,  2,  3,  3,  3,  3, 30, 30, 30, 30,  4,  4,  4,  4,  7,
        7,  7,  7,  5,  5,  5,  5, 23, 23, 23, 23, 33, 33, 33, 33, 36, 36,
       36, 36,  1,  1,  1,  1, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
       12, 15, 15, 15, 15,  8,  8,  8,  8, 27, 27, 27, 27, 20, 20, 20, 20,
       22, 22, 22, 22,  6,  6,  6,  6, 16, 16, 16, 16, 25, 25, 25, 25, 14,
       14, 14, 14, 26, 26, 26, 26])

array1の最初の要素である6は17に変換されている。
これはarray2の7番目の6という値とarray3の7番目の17という値が対応している。
このような変換をすべてのarray2,array3に対して行ったのが上記の配列である。

array4 = np.empty(len(array1),int) #array1と同じ次元の配列を作成
for i in range(40):
    array5 = np.where(array1 == i)[0] #iがarray1で何番にあるかを算出
    array4[array5] = array3[i] #array4においてarray5の番号にあたる数値をすべてarray3[i]に変換

array4
array([17, 17, 17, 17, 35, 35, 35, 35, 13, 13, 13, 13, 31, 31, 31, 31, 18,
       18, 18, 18, 28, 28, 28, 28, 37, 37, 37, 37, 29, 29, 29, 29,  9,  9,
        9,  9, 38, 38, 38, 38, 19, 19, 19, 19, 34, 34, 34, 34, 21, 21, 21,
       21, 32, 32, 32, 32,  0,  0,  0,  0, 24, 24, 24, 24, 39, 39, 39, 39,
        2,  2,  2,  2,  3,  3,  3,  3, 30, 30, 30, 30,  4,  4,  4,  4,  7,
        7,  7,  7,  5,  5,  5,  5, 23, 23, 23, 23, 33, 33, 33, 33, 36, 36,
       36, 36,  1,  1,  1,  1, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
       12, 15, 15, 15, 15,  8,  8,  8,  8, 27, 27, 27, 27, 20, 20, 20, 20,
       22, 22, 22, 22,  6,  6,  6,  6, 16, 16, 16, 16, 25, 25, 25, 25, 14,
       14, 14, 14, 26, 26, 26, 26])

コメントいただきました!!

array4 = array3[array1]
array4
array([17, 17, 17, 17, 35, 35, 35, 35, 13, 13, 13, 13, 31, 31, 31, 31, 18,
       18, 18, 18, 28, 28, 28, 28, 37, 37, 37, 37, 29, 29, 29, 29,  9,  9,
        9,  9, 38, 38, 38, 38, 19, 19, 19, 19, 34, 34, 34, 34, 21, 21, 21,
       21, 32, 32, 32, 32,  0,  0,  0,  0, 24, 24, 24, 24, 39, 39, 39, 39,
        2,  2,  2,  2,  3,  3,  3,  3, 30, 30, 30, 30,  4,  4,  4,  4,  7,
        7,  7,  7,  5,  5,  5,  5, 23, 23, 23, 23, 33, 33, 33, 33, 36, 36,
       36, 36,  1,  1,  1,  1, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12,
       12, 15, 15, 15, 15,  8,  8,  8,  8, 27, 27, 27, 27, 20, 20, 20, 20,
       22, 22, 22, 22,  6,  6,  6,  6, 16, 16, 16, 16, 25, 25, 25, 25, 14,
       14, 14, 14, 26, 26, 26, 26])

for文なしでできます。笑

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away