2024-05-04 02:17:01 发布
网友
Possible Duplicate:What is the easiest way to convert list with str into list with int?
当前数组:['1','-1','1'] 所需数组:[1,-1,1]
['1','-1','1']
[1,-1,1]
List comprehensions是前进的方向(请参见@sepp2k's answer)。使用^{}的可能替代方案:
map(int, ['1','-1','1'])
在列表理解中使用int将字符串转换为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
编辑:其他回答似乎效果更好
List comprehensions是前进的方向(请参见@sepp2k's answer)。使用^{} 的可能替代方案:
在列表理解中使用
int
将字符串转换为int,如下所示:看看我是否记得Python
编辑:其他回答似乎效果更好
相关问题 更多 >
编程相关推荐