Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn NumPy Array of characters into a string

I have a numpy array of characters and when I write it to file it writes as:

['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L']

I want it to write with just the letters and without the brackets or quotations i.e. as:

KRKPTTKTKRGL 

I have looked at numpy documentation and from what I have gathered the solution is a chararray however it looks like this is not as functional as a normal array.

Any help would be great. Thanks!

like image 521
aatakan Avatar asked Dec 19 '14 18:12

aatakan


1 Answers

If you just have a numpy array then why not convert it to a string directly for writing to your file? You can do this using str.join which accepts an iterable (list, numpy array, etc).

import numpy as np

arr = np.array(['K', 'R', 'K', 'P', 'T', 'T', 'K', 'T', 'K', 'R', 'G', 'L'])

s = ''.join(arr)
# KRKPTTKTKRGL
like image 119
Ffisegydd Avatar answered Sep 24 '22 16:09

Ffisegydd