## 将整数字符串列表转换为整数数组的最有效方法内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (233)

``````import numpy as np
print("Hello StackOverflow")

listOfStringOfINTs = ["123231231231231"]*5
print(listOfStringOfINTs)
numpyVectorOfInts = np.empty([len(listOfStringOfINTs),len(listOfStringOfINTs[0]) ], dtype='int')
for i, IntString in enumerate(listOfStringOfINTs):
numpyVectorOfInts[i] = list(map(int, IntString))

print(numpyVectorOfInts)
``````

### 2 个回答

``````    >>> k = [list(x) for x in listOfStringOfINTs ]
>>> print(np.array(k, dtype=np.int64))
[[1 2 3 2 3 1 2 3 1 2 3 1 2 3 1]
[1 2 3 2 3 1 2 3 1 2 3 1 2 3 1]
[1 2 3 2 3 1 2 3 1 2 3 1 2 3 1]
[1 2 3 2 3 1 2 3 1 2 3 1 2 3 1]
[1 2 3 2 3 1 2 3 1 2 3 1 2 3 1]]
``````

``````In [68]: np.array([list(astr) for astr in listOfStringOfINTs],int)
Out[68]:
array([[1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1],
[1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1],
[1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1],
[1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1],
[1, 2, 3, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1]])
``````

`list(astr)`将字符串拆分为1个字符串的列表。在`np.array``int`D型转换需要的所有字符串的照顾。

``````np.array(list(''.join(listOfStringOfINTs)),int).reshape(5,-1)
``````