-1

So say I have a 0-d array like this:

a = [3.0, 0.0, 3.0, 0.0, 5.0]

for each item in this array, I want to convert it back to a certain string value depending on the number. So for 0.0, I want to return 'no'. For 3.0, I want to return 'maybe'. And for 5.0, I want to return 'yes'. So it would look like this:

a = ['maybe', 'no', 'maybe', 'no', 'yes']

my problem is when I try to write a for loop to convert each item, I don't think I can use enumerate since all of the items are held in a 0-d array. I'm not sure how else I can go about updating this type of array.

  • what is the structure giving you that 0 is no and 3 is maybe and 5 is yes? if it is a dictionary (or maybe you can easily create one from your structure), then [this](https://stackoverflow.com/questions/3403973/fast-replacement-of-values-in-a-numpy-array) could help. If you want more something like between 0 and 1 it is no, between 1 and 4 is it maybe, between 4 and 5 it is yes, then you should look into binning the array, In pandas you can use [cut](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html), not sure of the equivalent in numpy though – Ben.T May 29 '20 at 01:47
  • it is not a 0d array. Your array is a 1-D array. I am not sure why you are referring to it as a 0-D array. could you please elaborate on that? thank you – Ehsan May 29 '20 at 03:48
  • That doesn't look like an array at all. It looks like a list. – user2357112 May 29 '20 at 04:21

1 Answers1

0

Are you looking for this:

a = np.array([3.0, 0.0, 3.0, 0.0, 5.0])
dictionary = {0:'no',3:'maybe',5:'yes'}
[dictionary[i] for i in a]

output:

['maybe', 'no', 'maybe', 'no', 'yes']
Ehsan
  • 12,072
  • 2
  • 20
  • 33