1

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']]
2
  • 1
    I have to ask: why? Commented Jun 1, 2022 at 20:10
  • I am doing it in language modeling to train vec2word model. Commented Jun 2, 2022 at 10:59

2 Answers 2

2

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')
Sign up to request clarification or add additional context in comments.

Comments

1

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']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.