4

I have an array produced by numpy which looks as follows:

[ 54.51399994 -12.10200024 -11.88099957]
[ 56.23899841 -8.30799961 -2.03500009]

How do i convert this to a list? So it looks as follows:

['54.51399994','-12.10200024','-11.88099957']
['56.23899841','-8.30799961','-2.03500009']

3
  • why do you have quotation in the second ones? Do you want to convert from number to string? Commented Nov 2, 2011 at 11:38
  • Yes, because I'm going to write this out to a text file Commented Nov 2, 2011 at 11:38
  • 5
    If you are dumping the array to a file, use np.savetxt. You do not need to do this conversion yourself! Commented Nov 2, 2011 at 11:44

1 Answer 1

5

You could use astype() to create a new array of string dtype:

import numpy as np
arr=np.array([
    ( 54.51399994, -12.10200024, -11.88099957),
    ( 56.23899841, -8.30799961, -2.03500009)])
print(arr.astype('|S10'))

yields

[['54.51399994', '-12.10200024', '-11.88099957'], ['56.23899841', '-8.30799961', '-2.03500009']]
Sign up to request clarification or add additional context in comments.

1 Comment

When you print a numpy array, it nicely formats the array without the commas. When you define the array with np.array, you have to use commas to separate the items in the tuples. So, yes, the arr.astype('|S10') should work on your array.

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.