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

  • Home
  • SEARCH
  • 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 9115163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:21:56+00:00 2026-06-17T04:21:56+00:00

I have a function that is supposed to do the following: take in a

  • 0

I have a function that is supposed to do the following: take in a list of lists, and a list of numerical weights. Then, take the weighted average of every bottom-level item in the list of lists, with the first item in the list of weights being used to weight the first list of things, the second item in the list of weights being used to weight the second list, etc. It’s like a function that simply takes the weighted average of items in a list, but the items are grouped so that a particular weight gets applied to each group. This is useful if a lot of things have the same weight. Here is the code:

def getAverage(x,wts=[0.1,0.3,0.6]):
    """Get weighted average of partitioned list."""
    xsum = 0
    i = 0
    for item in x:
        xsum += reduce(lambda x,y:x+y,item)*wts[i]
        i += 1
    return xsum/reduce(lambda x,y:x+y,wts)

However, when I try to compile, I get the following error for that line:

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

What? Why? Why does that multiplication return a list? That makes no sense. reduce() returns a number (integer to be precise), and the elements of wts are floats. How is it not allowed to be added to xsum? Did I misuse lambda or something?

  • 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-17T04:21:57+00:00Added an answer on June 17, 2026 at 4:21 am

    Your function is mainly sound (although I presume it’s either named wrong or you want to divide by the number of items at the end too – otherwise it’s not an average), so you must be passing in incorrect arguments. The most likely is that your second argument is in fact a list of lists, not a list of integers.

    This said, we can improve on the function. There are a few issues here. Firstly, you are using reduce() to sum items, when the sum() built-in can do this job more effectively.

    Next, the loop counts using i – this is a bad practice, instead, we should use the enumerate() built-in. However, here we are using it to loop over two lists at the same time, and in Python, that is best done using the zip built-in.

    As the result is being accumulated, we can turn the whole thing into a generator expression and sum that too. This means we can end up with a simple way of doing this:

    def weighted_average(items, weights):
        total = sum(sum(item)*weight for item, weight in zip(items, weights))
        return total/sum(weights)
    

    I also left out the default value for weights – unless the default is domain-specific, I would suggest there is no good default here, as it depends on the length of items. The only potential for a good default might be to presume equal weighting. E.g:

    def weighted_average(items, weights=None):
        if not weights:
            weights = [1]*len(items)
        total = sum(sum(item)*weight for item, weight in zip_longest(items, weights))
        return total/sum(weights)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function that is supposed to trigger anytime one of the
So I have the following function on a page that is supposed to be
I have the following function that is suppose to take a value from a
I have the following function that is supposed to get all elements in a
I have a query that's supposed to do the following: Take two sets of
I have a function in a powershell script that is supposed to untar my
Suppose that you have a function f: List a -> a such that f
On the following page I have a popup that is supposed to show beside
I have a JavaScript function that is supposed to generate a <div> dynamically and
In Excel, I have the following formula =(MIN(H69,H52,H35,H18)*(1/H18))*10 that is supposed to return the

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.