Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:50:37+00:00 2026-06-06T12:50:37+00:00

I have to calculate the average of each column in a specific nested list

  • 0

I have to calculate the average of each column in a specific nested list and then save the average into a new list. In my code so far, I have set the original list into a nested list and transposed it to read in the columns. I am just not sure how to code the average.

#First open the data text file
import re

f = open('C:\Python27\Fake1.txt', 'r')

#Convert to a nested list
nestedlist = []
q = f.read()
f.close()
numbers = re.split('\n', q) #Splits the \n and \t out of the list
newlist = []
for row in numbers:
   newlist.append(row.split('\t'))


#Reading in columns
def mytranspose(nestedlist):
   list_prime = []
   for i in range(len(nestedlist[0])):
          list_prime.append([])
   for row in nestedlist:
          for i in range(len(row)):
                 list_prime[i].append(row[i])
   return(list_prime)
print (mytranspose(newlist))


#Average of Columns
def myaverage(nestedlist):
   avg_list = []
   a = 0
   avg = 0
   for i in newlist:
          a = sum(newlist[i])
          avg = a/len(row)
          avg_list.append(avg[i])
   return(list_prime)

print(myaverage(newlist))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:50:39+00:00Added an answer on June 6, 2026 at 12:50 pm

    Here is a simpler way to do the whole thing:

    with open('C:\Python27\Fake1.txt', 'r') as f:
        data = [map(float, line.split()) for line in f]
    
    num_rows = len(data)
    num_cols = len(data[0])
    
    totals = num_cols * [0.0]
    for line in data:
        for index in xrange(num_cols):
            totals[index] += line[index]
    
    averages = [total / num_rows for total in totals]
    print averages
    

    I would however recommend using numpy for this sort of thing as it becomes trivial (as well as much faster):

    import numpy as np
    data = np.loadtxt('C:\Python27\Fake1.txt')
    print data.mean(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate the average value for each month. Currently I have 2
I have a data set that I need to calculate the trait average of
I have code to calculate the percentage difference between 2 numbers - (oldNum -
I have written a method to calculate a given percentile for a set of
I have a particular application that needs to calculate something very specific, and while
I have a list of states, major cities in each state, their populations, and
I have a file where each line is a set of results collected in
I have large matrix, 4000x4000 I need to calculate local average of 11x11 window
i have a function to calculate several different values: int ASPfile::get_dimensions(int* Lat, int* Lon,
I have two fields which calculate a total, but I have a radio button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.