How to convert 2D string list to 2D int list python?
本问题已经有最佳答案,请猛点这里访问。
如何转换二维int列表中的二维字符串列表?例子:
1 2 3 | >>> 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]] |
做清单理解,
1 2 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] |
结果
1 2 | In [26]: print result [[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]] |
号
Python3.x
1 | print ( [list( map(int,i) ) for i in l] ) |
输出:
1 | [[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]] |
。
python有一个名为int()的函数,可以将字符串转换为整数。试试看。