Most elegant way to modify elements of nested lists in place

前端 未结 7 746
囚心锁ツ
囚心锁ツ 2020-12-14 01:20

I have a 2D list that looks like this:

table = [[\'donkey\', \'2\', \'1\', \'0\'], [\'goat\', \'5\', \'3\', \'2\']]

I want to change the la

7条回答
  •  星月不相逢
    2020-12-14 01:45

    Try:

    >>> for row in table:
    ...     row[1:]=map(int,row[1:])
    ... 
    >>> table
    [['donkey', 2, 1, 0], ['goat', 5, 3, 2]]
    

    AFAIK, assigning to a list slice forces the operation to be done in place instead of creating a new list.

提交回复
热议问题