variance of list values [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to calculate the variance of values in list x1. Could anyone fix the error in this code?!
def my_mean(L):
s = 0
for i in range(0, len(L)):
s = s + L[i]
return s / len(L)
def my_var(L):
t = 0
for i in range(0, len(L)):
t = t + L[i] - def my_mean(L)
return t*t / len (L)
x1 = [1, 3, 4, -3, 8]
v1 = my_var(x1)
print(v1)

You need to use the def keyword only when you define the function.
When you call to the function you don't need to use def again.
Fix this row:
t = t + L[i] - def my_mean(L)
To:
t = t + L[i] - my_mean(L)

Related

Compare empty array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm writing a program that takes a list of floating point numbers representing distance in inches and converts them to shoe sizes to the nearest 0.5.
I'm trying to compare an empty list and expect the output to be The list is empty. Instead I don't return any value. Not sure where I'm going wrong?
foot_length = []
for length in foot_length:
if length == 0:
print('The list is empty.')
else:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)
I've tried a number of different approaches none of which get the output I want/expect.
if not length:
print('The list is empty.')
if len(length) == 0:
print('The list is empty.')
Try to change your code to
foot_length = []
if len(foot_length) == 0:
print('The list is empty.')
else:
for length in foot_length:
convert_to_shoe_size = length * 3 - 23
round_shoe_size = round(convert_to_shoe_size * 2, 2) / 2
print(round_shoe_size)

How to code this condition in Python? I am new to python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am new to python. so any help will be appreciated.
I have two arrays A = [1,2,4,2,3,5,3] and B = [0,4,4,4,1,1,1]
for the function if I give A, B as input then I should get output as = [1,(2+4+2),(3+5+3)] = [1,8,11](if numbers are repeating in B then corresponding values in A should be added together).
This should do the trick:
def bla(list1:list, list2:list):
prev = list2[0] - 1
final_list = []
for ele, pos in zip(list1, list2):
if prev != pos:
final_list.append(ele)
else:
final_list[-1] += ele
prev = pos
return final_list

Python The inner product is <function innerproduct at 0x000001EF5A6A7D30> error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My code is below, but I'm getting this weird output for my inner product code and don't know why it's not calculating the correct inner product. nums and nums2 ask the user for an equal list of numbers in which the inner product will be calculated. Any assistance would be appreciated.
def innerproduct(nums, nums2):
sum = 0.0
for i in range(len(nums)):
sum += nums[i] * nums2[i]
return innerproduct
The error arising because of the return innerproduct statement since that is the name of the function.
Instead, did you mean to return the sum?
def innerproduct(nums, nums2):
sum = 0.0
for i in range(len(nums)):
sum += nums[i] * nums2[i]
return sum
That's not "weird output", it's exactly what you told it to return. You ignored the result and returned a reference to the function object.
Try this instead:
result = 0
for i in range(len(nums)):
result += nums[i] * nums2[i]
return result
Note: do not give a variable the same name as a built-in type or function.
You can do this more directly with the built-in sum function:
return sum(nums[i] * nums2[i] for i in range(len(nums)))
Or perhaps even better:
return sum(a * b for a, b in zip(nums, nums2[i]))

How to fix the recursive function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
It's a simple question of recursive function, which extracting every digits from a positive number. As the comment mentioned, I have known the mistake is because of global variable, but I still have no idea to fix it to get expected result. Thanks.
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5,1,2,0]
But the expected output should be
print(getdigits(120)) # expected output = [1,2,0]
print(getdigits(5)) # expected output = [5]
You are using the existing LIST that already have values from the previous function call.
First clear the list and then try to call for another value.\
Try it
def getdigits(n):
if n == 0:
return list_1[::-1]
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120))
list_1.clear() #clear list values
print(getdigits(5))
def getdigits(n):
global list_1
if n == 0:
listTemp = list_1
list_1 = []
return listTemp
list_1.append(n % 10)
return getdigits(int(n / 10))
list_1 = []
print(getdigits(120)) # output = [1,2,0]
print(getdigits(5)) # output = [5]
you need to declare list 1 as a global varible inside your function so you can clear list one within th function
i have tested it and got that output

Python invalid Syntax Why?c [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Why is this invalid syntax?
if 0.9*x < d[o] < 1.1*x:
Here's the whole code
def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)
The line you quoted isn't the source of your error. This line is:
x = (500/(2**(i))*10**-6
Note the mismatched parentheses.
def phipsd(d,p):
a=[]
lend = len(d)
ad=np.array(d)
for i in range(0,9):
for o in range(0, len(d)):
x = (500/(2**(i))*10**-6 # Here is a SyntaxError, Because You've started 3 parentheses but terminated only 2. So, add a closing parenthesis in the right place.
if 0.9*x < d[o] < 1.1*x:
c = c + p[o]
a.append([])
b=a[i]
b.append(c)
See the comment

Categories