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 7004113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:09:26+00:00 2026-05-27T21:09:26+00:00

What would be the easiest/shortest/ most-Pythonic way to get the weighted average of a

  • 0

What would be the easiest/shortest/most-Pythonic way to get the weighted average of a and b?

a = (
        {
          0: {'P': 0.3, 'Z': 0.3, 'N': 0.3}, 
        'P': {'P': 0.9, 'Z': 0.1, 'N': 0.0}, 
        'Z': {'P': 0.1, 'Z': 0.9, 'N': 0.1}, 
        'N': {'P': 0.0, 'Z': 0.1, 'N': 0.9}
        },

        {
         'P': {'A': 0.3, 'C': 0.3, 'T': 0.2, 'G': 0.2}, 
         'Z': {'A': 0.3, 'C': 0.3, 'T': 0.2, 'G': 0.2}, 
         'N': {'A': 0.3, 'C': 0.3, 'T': 0.2, 'G': 0.2}
        }
    )

b = (
        {
          0: {'P': 0.3, 'Z': 0.3, 'N': 0.3}, 
        'P': {'P': 0.3, 'Z': 0.3, 'N': 0.3}, 
        'Z': {'P': 0.3, 'Z': 0.3, 'N': 0.3}, 
        'N': {'P': 0.3, 'Z': 0.3, 'N': 0.3}
        },

        {
         'P': {'A': 0.25, 'C': 0.25, 'T': 0.25, 'G': 0.25}, 
         'Z': {'A': 0.25, 'C': 0.25, 'T': 0.25, 'G': 0.25}, 
         'N': {'A': 0.25, 'C': 0.25, 'T': 0.25, 'G': 0.25}
        }
    )

So that the resulting c = [(a * a_weight) + (b * b_weight) / (a_weight + b_weight)] should have the same structure (as a and b).

Should anyone know the Pythonic answer, thank you.

Example of the resulting c structure

For instance, if a_weight = b_weight = 0.5 (a simple average), then c‘s first 'P': line

'P': {'P': (0.9 + 0.3) / 2, 'Z': (0.1 + 0.3) / 2, 'N': (0.0 + 0.3) / 2}

would be an average of

a‘s 'P': {'P': 0.9, 'Z': 0.1, 'N': 0.0} and
b‘s 'P': {'P': 0.3, 'Z': 0.3, 'N': 0.3}.

Basically, just an average of values (while every key stays as is).

  • 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-05-27T21:09:27+00:00Added an answer on May 27, 2026 at 9:09 pm

    I’d make a function that accepts two inputs and converts them recursively according to their type:

    def combine(a, b, a_weight, b_weight):
        if isinstance(a, tuple):
            return tuple(combine(x,y,a_weight,b_weight) for x,y in zip(a,b))
        elif isinstance(a, dict):
            return dict((k, combine(a[k],b[k],a_weight,b_weight)) for k in a)
        #add other data structures here if you need to (e.g. list, set, etc.)
        else: #assume a number
            return ((a * a_weight) + (b * b_weight)) / (a_weight + b_weight)
    

    The nice thing about this method is that it will keep working even if you change the structure of the inputs.

    Note that this code assumes that the structure of a and b is the same, the tuples have the same number of elements, and the dictionaries have the same keys. If this is not always the case you’d need to add code to verify this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the easiest way to detach a specific JPA Entity Bean that
What would be the easiest way to have a page where it will display
What would be the easiest way to have the color of a text field
What would be the easiest way to separate the directory name from the file
What would be the easiest way to be able to send and receive raw
What would be the easiest way to move the mouse around (and possibly click)
What would be the easiest way of using commands in the code to programatically
What would be the easiest or recommended way for making an NSOutlineView indent multiple
What is the easiest-to-code-in technology that would give me real time access to a
I'm looking for the quickest way to return a set of accounts that have

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.