首页 > 解决方案 > 将一个列表上的值归因于另一个列表

问题描述

我正在编写一个循环,从不同的工作表名称中提取数据,然后绘制这些数据。工作表名称在下面列出的代码中表示为“工作表名称”。每个工作表名称都与下面显示的列表“ts”中的数字一起出现。当我通过循环进行绘图时,我需要 x 值是 'ts' 中对应于 'sheetname' 的值。对于第一个,在我将它放入循环之前,我能够为 x 值输入一个“0”,但我显然不能再这样做了,因为第一张纸的值仅为 0。我的完整代码很长,但下面是“sheetnames”、“ts”和我的情节信息。请注意,工作表名称中包含的数据只是我的代码从 Excel 文件中提取数据的工作表。如果需要更多信息,

sheetnames = ['0h UV Exposure, Roll Direction', 
          '4h UV Exposure, Roll Direction', 
          '8h UV Exposure, Roll Direction', 
          '12h UV Exposure, Roll Direction', 
          '18h UV Exposure, Roll Direction', 
          '24h UV Exposure, Roll Direction', 
          '36h UV Exposure, Roll Direction', 
          '48h UV Exposure, Roll Direction', 
          '68h UV Exposure, Roll Direction', 
          '78h UV Exposure, Roll Direction', 
          '82h UV Exposure, Roll Direction', 
          '96h UV Exposure, Roll Direction'] 

ts = [0,4,8,12,18,24,36,48,68,78,82,96]

plt.figure()
plt.plot(0,Average_Toughness,'ro')
plt.xlabel('Time (Hours)')
plt.ylabel('Toughness')
plt.title('Toughness Plot')

标签: python

解决方案


sheetnames您可以使用range()迭代列表position,然后访问两个列表中的当前位置项,而不是直接迭代:

for pos in range(len(sheetnames)):
    # now you can use sheetnames[pos] and ts[pos]

推荐阅读