Home > Blockchain >  how to increase list valuesby list average value
how to increase list valuesby list average value

Time:01-27

I want to increase my values in list by average value of list.

lista1 = [2,3,4,5,6]

dlugoscListy = len(lista1)
sumaListy = sum(lista1)
srednia = sumaListy / dlugoscListy
lista2 = [i for i in lista1  = srednia]

print(lista2)

My code looks like above, but it is not working

CodePudding user response:

The syntax in your list comprehension is wrong. It needs to look like this:

list2 = [element   list_avg for element in my_list]

The expression has to be at the start, not the end.

Also you shouldn't use i in this case, as you are not counting numbers in a range, but using a for _ in loop and dealing with actual values from the list. You should call it "element" or "value" instead. This can easily lead to confusion, especially if you deal with lists which contain numbers, like in your case.

CodePudding user response:

The numpy library can also do it very well (and fast).

import numpy as np

lista1 = np.array([2, 3, 4, 5, 6])
lista2 = lista1   np.mean(lista1)
print(lista2)
  • Related