For循环-每次迭代更改索引

时间:2020-04-23 16:29:50

标签: loops for-loop indexing iteration

我有一个列表列表,其中包含客户,销售量和销售的线程,我需要将每个列表附加到新列表中:

for x1 in trans_clean:
  index = 0 
  customers.append(trans_clean[index]) 
  sales.append(trans_clean[1])
  thread_sold.append(trans_clean[2])

客户需要从索引0开始并跳4,因此0、4、8、12、16等。 销量为1 thread_sold at 2

1 个答案:

答案 0 :(得分:0)

我不确定您使用的是哪种语言,但这听起来像是您尝试在每次使用索引时对其进行迭代。这应该可以达到您的目的:

index = 0
for x1 in trans_clean: 
  customers.append(trans_clean[index]) 
  index = index + 4
  sales.append(trans_clean[1])
  thread_sold.append(trans_clean[2])

如果我知道您使用的是哪种语言,我可能可以为您提供更简洁的语法。例如,您也许可以使用要遍历的数组的索引乘以4。但是我认为我在做什么的概念应该很有意义。