Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert 2D string list to 2D int list python? [duplicate]

How to convert a 2d string list in 2d int list? example:

>>> 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]]
like image 232
srky Avatar asked Oct 24 '25 19:10

srky


2 Answers

Python 3.x

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

Output :

[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]
like image 199
Transhuman Avatar answered Oct 27 '25 08:10

Transhuman


Do with list comprehension,

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]

Result

In [26]: print result
[[1, 1, 3], [2, 3, 5], [3], [4, 5], [5, 1], [6, 6], [7]]
like image 41
Rahul K P Avatar answered Oct 27 '25 08:10

Rahul K P