1つのオプションは、scipy.sparse.coo_matrix
を使用して、スパース行列を作成し、密に変換することができます
from scipy.sparse import coo_matrix
lst = ['hello', 'world!!']
idx, idy, val = zip(*((i, j, ord(c)) for i, s in enumerate(lst) for j, c in enumerate(s)))
coo_matrix((val, (idx, idy)), shape=(max(idx)+1, max(idy)+1)).todense()
#matrix([[104, 101, 108, 108, 111, 0, 0],
# [119, 111, 114, 108, 100, 33, 33]])
またはitertools
からizip_longest(python2)/zip_longest(python3)
を使用します。
from itertools import izip_longest
list(zip(*izip_longest(*map(lambda s: map(ord, s), lst))))
# [(104, 101, 108, 108, 111, None, None), (119, 111, 114, 108, 100, 33, 33)]
これは、A与えます2dリストあなたはNonesを埋めるためにfillvalue
パラメータを使用することができます。
list(zip(*izip_longest(*map(lambda s: map(ord, s), lst), fillvalue=0)))
# [(104, 101, 108, 108, 111, 0, 0), (119, 111, 114, 108, 100, 33, 33)]
あなたがnumpyのの文字列型のいずれかで 'str'代わりのndarrayの' list'を使用している特別な理由はありますか? – o11c
また、すべての値が 'dtype = uint8'に収まるときに' dtype = float'とすることで得られるとは思えません。 – o11c