Python 列表字符串转换整型

python中把列表中的字符串转成整型的几种方法

方法一:最基本的方法:通过轮训整个list来实现转换,具体代码如下:

test_list=['1','3','2','6','8']

print("Original list is: " + str(test_list))

out_list=test_list

for i in range(0,len(test_list)):
    out_list[i]=int(test_list[i])

print("Out list is: " + str(out_list))

方法二:使用list的复合运算:

test_list=['1','3','2','6','8']

print("Original list is: " + str(test_list))

out_list=[int(i) for i in test_list]
print("Out list is: " + str(out_list))

方法三: 使用map():

test_list=['1','3','2','6','8']

print("Original list is: " + str(test_list))

out_list=list(map(int,test_list))

print("Out list is: " + str(out_list))

运算结果:

Original list is: ['1', '3', '2', '6', '8']
Out list is: [1, 3, 2, 6, 8]

Last updated