python - 将一个列表上的值归因于另一个列表
问题描述
我正在编写一个循环,从不同的工作表名称中提取数据,然后绘制这些数据。工作表名称在下面列出的代码中表示为“工作表名称”。每个工作表名称都与下面显示的列表“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')
解决方案
sheetnames
您可以使用range()
迭代列表position,然后访问两个列表中的当前位置项,而不是直接迭代:
for pos in range(len(sheetnames)):
# now you can use sheetnames[pos] and ts[pos]
推荐阅读
- excel - OpenXML PowerTools:是否可以将 Excel 电子表格写入 .Net MemoryStream?
- ios - 如何知道 NavigationView 在 SwiftUI 中弹出?
- angular - 使用 Angular 开发测验 Web 应用程序?
- mysql - Mysql如何从集合中排除一个子集?
- python - 如何在 Python 中使用 Appium 从“x”和“y”坐标中获取 XPATH?
- react-native - 如何使用多个 React Native DateTimePicker
- reactjs - 在兄弟组件之间传递数据时如何执行函数?
- python - Python正则表达式如何在字母和数字之间插入连字符;并删除两个字母之间的连字符
- awk - AWK:通过匹配特定列,将信息从 file2 追加到 file1 的新列中的同一行
- python-3.x - Kivymd 对话框关闭