10. Find the arithmetic mean of the negative elements in the list and find the smallest
replace with an item.
def fun(L):
tot = 0
n = 0
i_min = None
for i, x in enumerate(L):
if x < 0:
tot += x
n += 1
if i_min is None or x < L[i_min]:
i_min = i
if n > 0:
mean = tot / n
L[i_min] = mean
L = [1, -2, 3, 4 -5, 6, -7, -8]
print("Initial list ", L)
fun(L)
print("Processed list", L)
Comments
Leave a comment