您可以直接创建一个numpy字符数组,例如:
b = np.array([ ['h','e','l','l','o'],['s','n','a','k','e'],['p','l','a','t','e'] ])
通常的数组技巧可以解决这个问题。
如果您有
a并且希望从中 生成 b,请注意:
list('hello') == ['h','e','l','l','o']
因此,您可以执行以下 *** 作:
b = np.array([ list(word) for word in a ])
但是,如果
a单词长度不等(例如
['snakes','on','a','plane']),您想对较短的单词做什么?您可以用最长的单词用空格填充:
wid = max(len(w) for w in a)b = np.array([ list(w.center(wid)) for w in a])
其中
string.center(width)垫有空格,居中放置字符串。您还可以使用
rjust或
ljust(请参阅字符串docs)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)