[英]convert string numpy array to a ascii numpy matrix
I have been looking for an efficient way for converting a string numpy array to a two dimensional ASCII matrix in python. 我一直在寻找一种在python中将字符串numpy数组转换为二维ASCII矩阵的有效方法。 So for this is the best that I could come up with
所以这是我能想到的最好的
def charArrayToAsciiMatrix(strNumpyArray):
for i in range(strNumpyArray):
if(i==0):
AsciiMatrix=numpy.matrix(ord[[c for c in strNumpyArray[i]]])
else:
AsciiMatrix=numpy.vstack(AsciiMatrix,ord[[c for c in strNumpyArray[i]]])
is there a efficient way of doing this? 有一种有效的方法吗?
How is this? 这怎么样?
import numpy as np
def add_word_as_ordinal(arr, word):
arr.extend([ord(ch) for ch in word])
return arr
def char_array_to_ascii_matrix(char_array):
result = np.matrix(reduce(add_word_as_ordinal, char_array, []))
result.shape = len(char_array), len(char_array[0])
return result
my_array = np.array("JustA Flesh Wound".split())
my_matrix = char_array_to_ascii_matrix(my_array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.