Python:如何将字符串数组转换为数字数组?

2024-05-04 02:17:01 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
What is the easiest way to convert list with str into list with int?

当前数组:['1','-1','1'] 所需数组:[1,-1,1]


Tags: thetoconvertiswith数组whatway
3条回答

List comprehensions是前进的方向(请参见@sepp2k's answer)。使用^{}的可能替代方案:

map(int, ['1','-1','1'])

在列表理解中使用int将字符串转换为int,如下所示:

desired_array = [int(numeric_string) for numeric_string in current_array]

看看我是否记得Python

list = ['1' , '2', '3']
list2 = []
for i in range(len(list)):
    t = int(list[i])
    list2.append(t)

print list2

编辑:其他回答似乎效果更好

相关问题 更多 >