List of list, converting all strings to int, Python 3

后端 未结 4 900
难免孤独
难免孤独 2020-12-21 09:38

I am trying to convert all elements of the small lists in the big list to integers, so it should look like this:

current list:
list = [[\'1\',\'2\',\'3\'],[\         


        
4条回答
  •  遥遥无期
    2020-12-21 10:32

    Nested list comprehension is the best solution, but you can also consider map with lambda function:

    lista = [['1','2','3'],['8','6','8'],['2','9','3'],['2','5','7'],['5','4','1'],['0','8','7']]
    
    new_list = map(lambda line: [int(x) for x in line],lista)
    # Line is your small list.
    # With int(x) you are casting every element of your small list to an integer
    # [[1, 2, 3], [8, 6, 8], [2, 9, 3], [2, 5, 7], [5, 4, 1], [0, 8, 7]]
    

提交回复
热议问题