Find the best solutions to your questions at Westonci.ca, the premier Q&A platform with a community of knowledgeable experts. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.
Sagot :
Answer:
In Python:
file = input("File name: ")
f=open(file,"r")
lines=f.readlines()
col1=[]
col2=[]
for x in lines:
col1.append(float(x.split(' ')[0]))
for x in lines:
col2.append(float(x.split(' ')[1].rstrip('\n')))
f.close()
sum1 = 0.0
sum2 = 0.0
for i in range(len(col1)):
sum1+=col1[i]
sum2+=col2[i]
print("Average of Column 1: %.2f" %(sum1/len(col1)))
print("Average of Column 2: %.2f" %(sum2/len(col2)))
Explanation:
This prompts the user for file name
file = input("File name: ")
This opens the file
f=open(file,"r")
This reads the lines of the file
lines=f.readlines()
This creates an empty list for column 1
col1=[]
This creates an empty list for column 2
col2=[]
The following iterations iterate through the lines and puts them in the respective column list
for x in lines:
col1.append(float(x.split(' ')[0]))
for x in lines:
col2.append(float(x.split(' ')[1].rstrip('\n')))
This closes the file
f.close()
This initializes the sum of column 1 to 0
sum1 = 0.0
This initializes the sum of column 2 to 0
sum2 = 0.0
This iterates through each list and adds up the list contents
for i in range(len(col1)):
sum1+=col1[i]
sum2+=col2[i]
This calculates prints the average of the first column to 2 d.p
print("Average of Column 1: %.2f" %(sum1/len(col1)))
This calculates prints the average of the second column to 2 d.p
print("Average of Column 2: %.2f" %(sum2/len(col2)))
Thanks for using our platform. We aim to provide accurate and up-to-date answers to all your queries. Come back soon. We hope this was helpful. Please come back whenever you need more information or answers to your queries. Keep exploring Westonci.ca for more insightful answers to your questions. We're here to help.