Update numpy array list of list values using a dictionary

Viewed 184

I have the following numpy array (list of list)

arr2 =np.array([[15,  2,  2,  0,  2,  2,  2,  2,  2,  2,  3,  2,  2,  2,  2,  2,  2,
        2,  0, 13,  2,  2,  2,  2,  2,  2,  2,  2,  0,  2, 15,  2,  2,  0,
        2,  2,  2,  2,  2,  2,  3,  2,  2,  2,  2,  2,  2,  2,  0, 13,  2,
        2,  2,  2,  2,  2,  2,  2,  0,  2,  2,  2,  2,  2,  2,  2,  2,  2,
        2,  2,  2,  2,  2,  2,  2,  2],[15,  2,  2,  0,  2,  2,  2,  2,  2,  2,  3,  2,  2,  2,  2,  2,  2,
        2,  0, 13,  2,  2,  2,  2,  2,  2,  2,  2,  0,  2, 15,  2,  2,  0,
        2,  2,  2,  2,  2,  2,  3,  2,  2,  2,  2,  2,  2,  2,  0, 13,  2,
        2,  2,  2,  2,  2,  2,  2,  0,  2,  2,  2,  2,  2,  2,  2,  2,  2,
        2,  2,  2,  2,  2,  2,  2,  2]])

and the following dictionary:

dict2={'B-art': 10,
 'B-eve': 11,
 'B-geo': 6,
 'B-gpe': 15,
 'B-nat': 12,
 'B-org': 5,
 'B-per': 16,
 'B-tim': 4,
 'I-art': 1,
 'I-eve': 0,
 'I-geo': 13,
 'I-gpe': 7,
 'I-nat': 9,
 'I-org': 8,
 'I-per': 14,
 'I-tim': 2,
 'O': 3}

My goal is to get a new list with the keys of the dictionary

Expected output:

[['B-gpe', 'I-tim' ,  'I-tim', ...]] etc ...

I'm trying the following code:

for key, value in dict2.items():
    if value not in arr2:
            continue
    index = np.where(arr2 == value)
    arr2[index] = key

but I'm getting the following error:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
ValueError: invalid literal for int() with base 10: 'B-gpe'
2 Answers

arr2 is an integer array, you shouldn't update it with a string. Try:

out = np.full(arr2.shape, '', dtype='O')
for key, value in dict2.items():
    index = np.where(arr2 == value)
    out[index] = key

Output:

[['B-gpe' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'O' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-eve' 'I-geo' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-eve' 'I-tim' 'B-gpe' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'O' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-eve' 'I-geo' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim']
 ['B-gpe' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'O' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-eve' 'I-geo' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-eve' 'I-tim' 'B-gpe' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'O' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-eve' 'I-geo' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-eve' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim' 'I-tim'
  'I-tim' 'I-tim' 'I-tim' 'I-tim']]

Please try

    strarr2=arr2.astype(str)
    for key, value in dict2.items():
        strarr2[strarr2==str(value)]=key
Related