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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:55:40+00:00 2026-05-26T02:55:40+00:00

This is an offshoot of a previous question which started to snowball. If I

  • 0

This is an offshoot of a previous question which started to snowball. If I have a matrix A and I want to use the mean/average of each row [1:] values to create another matrix B, but keep the row headings intact, how would I do this? I’ve included matrix A, my attempt at cobbling together a list comprehension, and the expected result.

from operator import sum,len

# matrix A with row headings and values
A = [('Apple',0.95,0.99,0.89,0.87,0.93),
('Bear',0.33,0.25.0.85,0.44,0.33),
('Crab',0.55,0.55,0.10,0.43,0.22)]

#List Comprehension
B = [(A[0],sum,A[1:]/len,A[1:]) for A in A]

Expected outcome

B = [('Apple', 0.926), ('Bear', 0.44), ('Crab', 0.37)]
  • 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-26T02:55:40+00:00Added an answer on May 26, 2026 at 2:55 am

    Your list comprehension looks a little weird. You are using the same variable for the iterable and the item.

    This approach seems to work:

    def average(lst):
        return sum(lst) / len(lst)
    
    B = [(a[0], average(a[1:])) for a in A]
    

    I’ve created a function average for readability. It matches your expected values, so I think that’s what you want, although your use of mul suggests that I may be missing something.

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

Sidebar

Related Questions

This is an offshoot of this question . How do I use a Login
This is an offshoot from a previous question, is this bad practice (using the
This post is an offshoot of my previous post . However I have not
This is an offshoot of this question: Chrome counts characters wrong in textarea with
this is probably the most noob question i have ever asked, i need to
This is what I want to do. I have an array. $arr = array('value1','value2','value3','value4','value5','value6');
This is an offshoot to my other question . Is there something special I
This is a general algorithm question. I want to run some shortest path algorithm
This question might have been asked many times but still there are many factors
This isn't strictly a homework question, but an offshoot. How are time functions implemented

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.