Home > other >  Convert ndarray to string array
Convert ndarray to string array

Time:06-02

Lets say there is array

values = [[ 116.17265886,   39.92265886,  116.1761427 ,   39.92536232],
          [ 116.20749721,   39.90373467,  116.21098105,   39.90643813],
          [ 116.21794872,   39.90373467,  116.22143255,   39.90643813]]

now I want to convert this to

values = [[ '116.17265886',   '39.92265886',  '116.1761427' ,   '39.92536232'],
          [ '116.20749721',   '39.90373467',  '116.21098105',   '39.90643813'],
          [ '116.21794872',   '39.90373467',  '116.22143255',   '39.90643813']]

CodePudding user response:

Assuming you really have a numpy array (not a list of list), you can use astype(str):

values = np.array([[ 116.17265886,   39.92265886,  116.1761427 ,   39.92536232],
                   [ 116.20749721,   39.90373467,  116.21098105,   39.90643813],
                   [ 116.21794872,   39.90373467,  116.22143255,   39.90643813]])

out = values.astype(str)

output:

array([['116.17265886', '39.92265886', '116.1761427', '39.92536232'],
       ['116.20749721', '39.90373467', '116.21098105', '39.90643813'],
       ['116.21794872', '39.90373467', '116.22143255', '39.90643813']],
      dtype='<U32')

CodePudding user response:

If it's not a numpy array and it is a list of a list of values the following code should work:

for index in range(len(values)):
   values[index] = [str(num) for num in values[index]]
print(values)

For each list it returns a list of each of the values changed to a string, this returns the following.

[['116.17265886', '39.92265886', '116.1761427', '39.92536232'], 
['116.20749721', '39.90373467', '116.21098105', '39.90643813'], 
['116.21794872', '39.90373467', '116.22143255', '39.90643813']]
  • Related