小编sti*_*gmj的帖子

如何快速将像"001100"这样的字符串转换为numpy.array([0,0,1,1,0,0])?

我有一个由0和1组成的字符串,就像'00101'.我想将它转换为numpy数组numpy.array([0,0,1,0,1].

我正在使用for循环:

import numpy as np
X = np.zeros((1,5),int)
S = '00101'
for i in xrange(5):
    X[0][i] = int(S[i])
Run Code Online (Sandbox Code Playgroud)

但由于我有很多字符串,每个字符串的长度是1024,这种方式非常慢.有没有更好的方法来做到这一点?

python format types numpy type-conversion

5
推荐指数
2
解决办法
164
查看次数

标签 统计

format ×1

numpy ×1

python ×1

type-conversion ×1

types ×1