[[y-m for y in q] for (q,m) in [[q,min(q)] for q in x]]
subtraction of a minimum value within an array in a list
-
01-07-2022 - |
Вопрос
Suppose I have a list
x = [[1,2,3],[5,4,20],[9,100,7]]
I want to something similar to:
xcor = x - min(x) #should return [[0,1,2],[1,0,16],[2,93,0]]
Решение
Другие советы
Well try exactly what you say.
Find the minimum value first and either edit the existing list or create a new array.
Something like this for the minimum:
def min_value(x):
min = x[0]
for i in range(len(x)):
if x[i] < min:
min = cell
return min
and either something this
def subtract_min_from_each_list(x):
for i in range(len(x)):
min = min_value(x[i])
for j in range(len(x[i])):
x[i][j] = x[i][j] - min
return x
or this
def get_subtracted_list(x):
new_x = []
for i in range(len(x)):
temp = []
min = min_value(x[i])
for j in range(len(x[i])):
temp.append(x[i][j] - min)
new_x.append(temp)
return new_x
to act on your list.
you can try following in python
x = [[1,2,3], [5,4,20], [9,100,7]]
for n in range(len(x)):
pivot = x[n][n]
for m in range(len(x[0])):
x[n][m] = x[n][m] - pivot
print x
Не связан с StackOverflow