如何将2D字符串列表转换为2D int list python?

时间:2017-07-03 11:56:48

标签: python string list int type-conversion

如何在2d int列表中转换2d字符串列表?例如:

>>> pin_configuration = [['1', ' 1', ' 3'], ['2', ' 3', ' 5'], ['3'], ['4', ' 5'], ['5', ' 1'], ['6', ' 6'], ['7']]

>>> to [[1,1,3], [2,3,5], [3], [4,5], [5,1], [6,6], [7]]

3 个答案:

答案 0 :(得分:3)

使用列表理解,

In [24]: l =  [['1', ' 1', ' 3'], ['2', ' 3', ' 5'], ['3'], ['4', ' 5'], ['5', ' 1'], ['6', ' 6'], ['7']]

In [25]: result = [map(int,i) for i in l]

结果

In [26]: print result
[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]

答案 1 :(得分:1)

Python 3.x

print ( [list( map(int,i) ) for i in l] )

输出:

[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]

答案 2 :(得分:0)

Python有一个名为int()的函数,它将字符串转换为整数。试试吧。