如何在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]]
答案 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()的函数,它将字符串转换为整数。试试吧。