【发布时间】:2017-01-26 18:05:05
【问题描述】:
我正在尝试为数据创建基线。我需要从每个列表中获取每列的平均值,并且有十个列表。每个列表有大约 50 个元素。通过每列取平均值会给我在道路上那个点的平均值,所以我需要小心不要取列表的平均值。我可以通过仅在文件名循环中建立索引来单独获取每一列,但这非常低效。然后我会使用 MatplotLib 绘制数据,但这部分应该很容易。这是我到目前为止的代码:
def graphWriterIRI():
n = 0
for filename in os.listdir(os.getcwd()):
# Initialize a new set of lists for each file
startList = []
endList = []
iriRList = []
iriLList = []
# Load the file
if re.search('BASELINE',filename):
with open(filename, 'rU') as file:
for row in csv.DictReader(file):
try:
startList.append(float(row['Start-Mi']))
endList.append(float(row[' End-Mi']))
except:
startList.append(float(row['Start-MP']))
endList.append(float(row[' End-MP']))
try:
iriRList.append(float(row[' IRI R e']))
iriLList.append(float(row['IRI LWP ']))
except:
iriRList.append(float(row[' IRI RWP']))
iriLList.append(float(row['IRI LWP ']))
print iriRList[0] # prints column[0] of the list but I need this for 50 rows and two lists.
这是我在代码中引入的一些数据:
Start-Mi End-Mi IRI LWP IRI R e
194.449 194.549 80.3 87.4
194.549 194.649 85.3 91.1
194.649 194.749 87.4 95.6
194.749 194.849 83.6 72.5
194.849 194.949 73.7 81
194.949 195.049 85.2 87.2
195.049 195.149 106.3 111.5
195.149 195.249 84.2 92.4
195.249 195.349 95.5 95.5
195.349 195.449 60.1 67.2
195.449 195.549 56.6 51.3
195.549 195.649 80.6 74.4
195.649 195.749 73.7 69.9
195.749 195.849 49.6 48.1
195.849 195.949 48.1 50.2
195.949 196.049 53.3 45.2
196.049 196.149 55.8 45.8
196.149 196.249 46.7 48.1
我特别想做的是获取每个文件的 iriRList 和 iriLList 中列的平均值,每个文件都是一个列表。
【问题讨论】:
-
您对每个文件都有一个
iriRList,并且您想要每个文件中第 i 列的平均值? -
是的,我有一个 iriRList 和一个 iriLList,我需要对每个文件中的每个相应数据点求平均值。这是我上传到 github 的一些文本文件。 github.com/thomasawolff/verification_text_data。我正在使用基线数据集。
-
所以我需要英里点 194.449 和 194.549 等文件之间所有数据点的平均值。
-
但是每个文件都是一个列表,我们使用的列表是 iriRList 和 iriLList
标签: python python-2.7 list