Enumerate list of elements starting from the second element

前端 未结 3 557
不知归路
不知归路 2021-01-21 13:46

I have this list

[[\'a\', \'a\', \'a\', \'a\'],
 [\'b\', \'b\', \'b\', \'b\', \'b\'],
 [\'c\', \'c\', \'c\', \'c\', \'c\']]

and I want to conca

3条回答
  •  执笔经年
    2021-01-21 14:12

    When you call

    enumerate(list_of_lines, start=1)
    

    , the pairs that it generates are not

    1 ['b', 'b', 'b', 'b', 'b']
    2 ['c', 'c', 'c', 'c', 'c']
    

    , but rather

    1 ['a', 'a', 'a', 'a']
    2 ['b', 'b', 'b', 'b', 'b']
    3 ['c', 'c', 'c', 'c', 'c']
    

    That is, the start value indicates what the first index used should be, not what the first element to use is.

    Perhaps an alternate way of doing this would be as follows:

    for (index, item) in list(enumerate(list_of_lines))[1:]:
        list_of_lines[index][1:3] = [''.join(item[1:3])]
    

提交回复
热议问题