Question

HI I would like to ask how to write the function when my list is [[1,2,4],[0,3,2]]. Like merging them. I managed to do it separately only. The mean for [1,2,4] is 2.33 and so xbar return me [-1.33,-0.33,1.67] (is like [1,2,4] minus the 2.33. I need help for more lists. Thanks

>>> def mean(Ist): return sum(Ist)/len(Ist) >>> def xbar(data): i = 0 a = mean(data) for i in range(len(data)): data[i] -= a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

Here is your answer, Here we can use exception handling properties of pythhon. When we passed through the single list of element, we can easly perform sum() operation. But when we are passing a array of list(2_D array) , we can't perform sum() operation it will produce the above error. So we can handle this problem using exception handling. If there is exception occured during the runtime, we can convert the array of list into a single list, and can find the sum() of elements and length.

So here is your code for fixed code.

def mean(lst):
    return sum(lst)/len(lst)
def xbar(data):
    """
    Just modified with exception handling,If exception occurred, we will convert the array into list.
    """
    try:
        i=0
        a=mean(data)
        for i in range(len(data)):
            data[i]-=a
            i+=1
        return data
    except:
        #create a new list 
        new_data=[]
        #iterate through elements in the array of list, and create a new list with all elements in the array of list
        for row in data:
                a=mean(row)
                for i in row:
                    new_data.append(i-a)
        return new_data
data1=[[1,2,4],[0,3,2]]
data2=[0,3,2]
print(xbar(data1))

output

print(xbar(datai)) [-1.3333333333333335, -0.3333333333333335, 1.6666666666666665, -1.6666666666666667, 1.3333333333333333, 0.

Any doubt please comment, i am here to help you

Thanks in advance

Add a comment
Know the answer?
Add Answer to:
HI I would like to ask how to write the function when my list is [[1,2,4],[0,3,2]]....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For my computer class I have to create a program that deals with making and searching...

    For my computer class I have to create a program that deals with making and searching tweets. I am done with the program but keep getting the error below when I try the first option in the shell. Can someone please explain this error and show me how to fix it in my code below? Thanks! twitter.py - C:/Users/Owner/AppData/Local/Programs/Python/Python38/twitter.py (3.8.3) File Edit Format Run Options Window Help import pickle as pk from Tweet import Tweet import os def show menu():...

  • Hi I am using python and I keep getting this error with my code. Traceback (most...

    Hi I am using python and I keep getting this error with my code. Traceback (most recent call last): ['1'] Traceback (most recent call last): File "/Users/isabellawelch/Python ish/shuffle.py", line 86, in <module> emp = Employee(emp_data[0], emp_data[1], emp_data[2], emp_data[3], emp_data[4]) IndexError: list index out of range What does it mean and how do i fix it?? from tkinter import Tk, Label, Button, Entry, END employees = [] class Employee: def __init__(self, empno, name, addr, hwage, hworked): self.empno = empno self.name =...

  • Using python 3.6 How do I incorporate 0 or negative input to raise an error and...

    Using python 3.6 How do I incorporate 0 or negative input to raise an error and let the user try again? like <=0 #loop to check valid input option. while True: try: choice = int(input('Enter choice: ')) if choice>len(header): print("Invalid choice!! Please try again and select value either:",end=" ") for i in range(1,len(header)+1): print(i,end=",") else: break choice = int(input('\nEnter choice: ')) except ValueError as ve:print('Invalid response, You need to select the number choice from the option menu') # Catch your...

  • Using Python3, How can I aggregate the output from each iteration to one single list? Because...

    Using Python3, How can I aggregate the output from each iteration to one single list? Because from the program I have right now, it gives me the result of every iteration and gives me something like this as individual results and not one large list: None None ... None 130.0 None ... 1764 1765 None To clarify, I have this program where it calculates the sum of bytes sent within each incrementation of every 1 second, but there are instances...

  • how to make my code of python work probely my q is how to write a...

    how to make my code of python work probely my q is how to write a code to find the average actual water level, average error by using python and my program should be flexible where user can select the column no. If you can see in the main program, it asked which column to know the maximum or average, the column number is passed to the function. The function should look which column to do. #THIS PROGRAMMING IS ABLE...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT