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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:24:45+00:00 2026-06-13T01:24:45+00:00

I will explain my issue using an example: A=[[1,2,10],[1,2,10],[3,4,5]] B=[[1,2,30],[6,7,9]] From these lists of

  • 0

I will explain my issue using an example:

A=[[1,2,10],[1,2,10],[3,4,5]]
B=[[1,2,30],[6,7,9]]

From these lists of lists, i would like to create a third one:

C=A+B

So i get :

C= [[1, 2, 10], [1, 2, 10], [3, 4, 5], [1, 2, 30], [6, 7, 9]]

Notice that there are three lists inside C ,
the [1, 2, 10], [1, 2, 10], [1, 2, 30] lists, which if described in terms of [x,y,z], they have the same x,y but different z.

So i would like to have this new list:

Averaged= [(1, 2, 16.666), (6, 7, 9), (3, 4, 5)]

where we find only one occurrence of the same x,y from lists

[1, 2, 30], [1, 2, 40], [1, 2, 50]

and the average of the corresponding z values (10+10+30)/3=16.666

I tried using for loops at the beginning but ended up trying to do this using defaultdict.

I ended up with this that keeps once the (x,y) but adds and not averages the corresponding z values:

from collections import defaultdict
Averaged=[]

A=[[1,2,10],[1,2,10],[3,4,5]]
B=[[1,2,30],[6,7,9]]
C=A+B
print "C=",C

ToBeAveraged= defaultdict(int)
for (x,y,z) in C:
    ToBeAveraged[(x,y)] += z
Averaged = [k + (v,) for k, v in ToBeAveraged.iteritems()]    

print 'Averaged=',Averaged

Is it possible to do this with defaultdict? Any ideas?

  • 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-13T01:24:48+00:00Added an answer on June 13, 2026 at 1:24 am

    You’ll need to sort the data first:

    >>> C = sorted(A + B)
    >>> def avg(x):
            return sum(x) / len(x)
    
    >>> [[avg(i) for i in zip(*y)] for x,y in 
         itertools.groupby(C, operator.itemgetter(0,1))]
    [[1.0, 2.0, 16.666666666666668], [3.0, 4.0, 5.0], [6.0, 7.0, 9.0]]
    

    If you just want the groups before the average:

    [list(y) for x,y in itertools.groupby(C, operator.itemgetter(0,1))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem and I will try to explain the issue: I have
I am running into an issue with field inheritance. First I will explain how
I am using aspx controls from dev express. The issue I am having is
I have a small issue and I will explain as best as I can.
Before delving into the issue, first I will explain the situation. I have two
Can you please explain what is happening here? using(DataSet ds = GetDataSet()) // will
First I will explain what I am trying to do. I am trying to
Ok this is strange but I will explain what is going on. In SQL
The question is complicated but I will explain it in details. The goal is
The title is a little confusing so I will explain. I have the following

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.