简体   繁体   English

枚举从第二个元素开始的元素列表

[英]Enumerate list of elements starting from the second element

I have this list我有这个清单

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

and I want to concatenate 2nd and 3rd elements in each row, starting from the second row, to make something like this:我想连接每一行中的第二个和第三个元素,从第二行开始,做这样的事情:

[['a', 'a', 'a', 'a'],
 ['b', 'bb', 'b', 'b'],
 ['c', 'cc', 'c', 'c']]

It seems to work fine, when I do it to every row:当我对每一行都这样做时,它似乎工作正常:

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

but when I'm starting from the second row - I have "list index out of range" error:但是当我从第二行开始时 - 我有“列表索引超出范围”错误:

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

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])]

You can explicitly create an iterable with the iter() builtin, then call `next(iterable) to consume one item.您可以使用内置的iter()显式创建一个可迭代对象,然后调用 `next(iterable) 来使用一项。 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.注意第一行的切片,一般来说,改变你正在迭代的东西是个坏主意,所以切片只是在构造迭代器之前克隆列表,所以原始的 list_of_lines 可以安全地改变。

There is not much merit here for using enumerate() ... you can simply .pop() the n-th item from inner lists.这里使用 enumerate() 没有太多优点……您可以简单地.pop()内部列表中的第 n 个项目。 For loop over your data, starting at index 1 and add the 2nd value (popped) to the 1st element of the inner list: For 循环数据,从索引 1 开始,将第二个值(弹出)添加到内部列表的第一个元素:

data = [['a', 'a', 'a', 'a'],
 ['b', 'b', 'b', 'b', 'b'],
 ['c', 'c', 'c', 'c', 'c']] 

for row in range(1,len(data)):  # apply to 1st to n-th inner list by index
    item = data[row].pop(2)         # remove the 2nd item from inner list
    data[row][1] += item            # add it to the 1st of inner list

print(data)

Output:输出:

[['a', 'a', 'a', 'a'], 
 ['b', 'bb', 'b', 'b'], 
 ['c', 'cc', 'c', 'c']]

See list.pop(index)见 list.pop(index)

You can simply do this using list slicing syntax like shown below您可以使用列表切片语法简单地执行此操作,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM