I have numpy array of 1,000 elements which I want to convert to strings.
I have tried:
map(str, a)
It’s very slow. Any other option? Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to write a numpy array to a text file, use
numpy.savetxt. Based on your comment, this is what you want.However, in the interest of answering your original question, there are faster ways to convert a numpy array to strings, if you can live with fixed-length strings.
For simple things, you can convert it to a fixed-length string array.
E.g.
This outputs:
This will be much faster, but you’re limited to fixed-length strings. (Obviously, you can change the length. Just use
x.astype('|S10')or whatever length you’d like.)Again, though, if you’re just wanting to write the data to a file, use
savetxt.