简体   繁体   English

Python通过循环递增数组索引

[英]Python incrementing array index through loop

Im currently using xlwings to display graphs and their corresponding values in microsoft excel. 我目前正在使用xlwings在microsoft excel中显示图形及其对应的值。 I have 5 graphs and their coordinates(in an array) that i've successfully been able to print through a loop. 我有5个图表及其坐标(在一个数组中),我已经成功地通过循环打印。 Unfortunately the columns in which they appeared in had to be hard coded and would be affected if i were to add more plots, so i changed my code to: 不幸的是,它们出现的列必须是硬编码的,并且如果我要添加更多的绘图会受到影响,所以我将我的代码更改为:

for i in range(1, 6):
    Columns = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"]

    Range(Columns[0] + str(1)).value = list(zip(Xvalues))
    Range(Columns[1] + str(1)).value = list(zip(Yvalues))

Currently it will take the first plot and print the x-coordinates vertically in Column A("A1") and then the y-coordinates also vertically in Column B("B1") and then continues. 目前,它将采用第一个绘图并在A列(“A1”)中垂直打印x坐标,然后在B列(“B1”)中垂直打印y坐标,然后继续。

My question is how can i increment the index of Columns[] within the loop so that my next values are Columns[3] and Columns[4]? 我的问题是如何在循环中增加Columns []的索引,以便我的下一个值是Columns [3]和Columns [4]?

You can use another variable to keep track of the column index. 您可以使用另一个变量来跟踪列索引。 In this case j : 在这种情况下j

Columns = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"]
j = 1
for j,i in enumerate(range(1, 5)):    
    Range(Columns[j] + str(1)).value = list(zip(Xvalues))
    Range(Columns[j+1] + str(1)).value = list(zip(Yvalues))
    j += 1

Also you should not declare the list of columns within the loop. 此外,您不应声明循环中的列列表。

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

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