Enumerate list of elements starting from the second element

前端 未结 3 582
不知归路
不知归路 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:02

    You can explicitly create an iterable with the iter() builtin, then call `next(iterable) to consume one item. Final result is something like this:

    line_iter = iter(list_of_lines[:])
    # consume first item from iterable
    next(line_iter)
    for index, item in enumerate(line_iter, start=1):
        list_of_lines[index][1:3] = [''.join(item[1:3])]
    

    Note the slice on the first line, in general it's a bad idea to mutate the thing you're iterating over, so the slice just clones the list before constructing the iterator, so the original list_of_lines can be safely mutated.

提交回复
热议问题