Tbh I’m completely lost so I’m just gonna post all three assignments - any help or input which commands to use will be most appreciated! :
- Create a function called
read_data
, that takes a filename as argument. The function should open the file, and read its contents into a list of list of floats, where the outer list corresponds to the lines of the file, and the inner lists correspond to the columns (that is, convert the strings of each line to a list of two numeric values, and append them to an outer list). The function should return this list. Test the function by calling it on the experimental_results.txt
file like this:
list_of_rows = read_data(‘experimental_results.txt’)
print(list_of_rows)
- Write a function called
calc_averages
that takes a list of list of floats as input, and calculates the average value for each column by iterating over the rows. The function should return these two values. Test the function by calling it like this:
col1_avg, col2_avg = calc_averages(list_of_rows)
print(col1_avg, col2_avg)
- Write a function called
transpose_data
that turns the list of lists around so that it becomes a list of columns, rather than a list of rows. This means that the outer list now has two elements each containing a list of all the values in the corresponding column. In other words, if I want to access the 26th value in the 2nd column, I would now index with [1][25] instead of [25][1]
This is my “progress” on assignment 1 so far. Quite lost if I’m on the right path.
You’ll need to address the “invalid syntax” error first. It’s pointing to line 1:
def read_data('experimental_results.txt'):
Your function parameters shouldn’t be strings like that, but rather something like:
def read_data(filename):
Notice that I left off the quotation marks. Then on line 5:
with open(filename) as f:
See if that helps. You may see other syntax errors after fixing the first, for example maybe your indent is not correct for the return line.
Thanks for the reply and help
I also got some other help, so got the problem solved. But really appreciate, cheers
It sounds like you’re working on a series of Python functions to manipulate data from a file! Here’s how you can structure your solutions for each function based on the requirements:
Function 1: read_data
This function will read the contents of a file and convert each line into a list of floats. Here’s a sample implementation:
def read_data(filename):
with open(filename, 'r') as file:
data = []
for line in file:
# Strip whitespace and split by comma (or space, depending on your file)
row = list(map(float, line.strip().split(','))) # Adjust delimiter as needed
data.append(row)
return data
# Test the function
list_of_rows = read_data('experimental_results.txt')
print(list_of_rows)
Function 2: calc_averages
This function calculates the averages for each column. Here’s how you could implement it:
def calc_averages(data):
col1_sum = col2_sum = 0
row_count = len(data)
for row in data:
col1_sum += row[0]
col2_sum += row[1]
col1_avg = col1_sum / row_count
col2_avg = col2_sum / row_count
return col1_avg, col2_avg
# Test the function
col1_avg, col2_avg = calc_averages(list_of_rows)
print(col1_avg, col2_avg)
Function 3: transpose_data
This function transposes the list of lists:
def transpose_data(data):
transposed = [[row[i] for row in data] for i in range(len(data[0]))]
return transposed
# Test the function
transposed_data = transpose_data(list_of_rows)
print(transposed_data)
Summary
With these functions, you should be able to read the data from your file, calculate averages, and transpose the data as needed. If you’re looking for python assignment help, make sure to break down each part of your assignments and test them individually to ensure everything works as expected. Good luck, and don’t hesitate to reach out if you have more questions!