将NumPy个字符数组转换为字符串 [英] Turn NumPy Array of characters into a string
本文介绍了将NumPy个字符数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个numpy的字符数组,当我将其写入文件时,它写为:
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
我看过numpy文档,从我那里收集到的解决方案是一个chararray,但是它看起来不像普通数组那样有用.
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!
推荐答案
您可以将 numpy
的 tostring()
方法用作:
>>> st = np.array(['K' 'R' 'K' 'P' 'T' 'T' 'K' 'T' 'K' 'R' 'G' 'L'])
>>> st.tostring()
'KRKPTTKTKRGL'
因为您有一个 numpy
数组,所以此方法将比 join()
更快.
Since you have a numpy
array, this method will be faster than join()
.
对于Python3x, tostring()
可以用作:
For Python3x tostring()
can be used as:
>>> st = np.array(['K','R','K','P','T','T','K','T','K','R','G','L'])
>>> st.astype('|S1').tostring().decode('utf-8')
'KRKPTTKTKRGL'
这篇关于将NumPy个字符数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文