Explore Westonci.ca, the leading Q&A site where experts provide accurate and helpful answers to all your questions. Connect with a community of experts ready to provide precise solutions to your questions quickly and accurately. Experience the ease of finding precise answers to your questions from a knowledgeable community of experts.
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're always here to provide accurate and up-to-date answers to all your queries. We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Stay curious and keep coming back to Westonci.ca for answers to all your burning questions.