Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function of difference between value

Is there a function in Python to get the difference between two or more values in a list? So, in those two lists:

list1 = [1, 5, 3, 7]
list2 = [4, 2, 6, 4]

I need to calculate the difference between every value in list1 and list2.

for i in list1:
    for ii in list2:
       print i -ii

This gives negative values, but I want the subtraction between the values of the two lists only from highest value to lowest value for not getting negative values.

For the above lists, I expect the output to be [3, 3, 3, 3].

Thanks.

like image 620
gho Avatar asked Dec 27 '15 23:12

gho


People also ask

What is the function of difference?

The DIFFERENCE() function compares two SOUNDEX values, and returns an integer. The integer value indicates the match for the two SOUNDEX values, from 0 to 4.

What is the difference between two functions?

The Difference of Two Functions Suppose we have two functions, f(x) and g(x). We can define the difference of these two functions by, (f − g)(x) = f(x) − g(x), where x is in the domain of both f and g.

What is the function of a value?

In a problem of optimal control, the value function is defined as the supremum of the objective function taken over the set of admissible controls. Given , a typical optimal control problem is to. subject to. with initial state variable .

What is the difference between value of function and limit of function?

The value of a function is the actual calculation done at a certain point. The limit is - roughly speaking - the value at points that are “arbitrarily close” to the same point. For most commonly used functions, the value of a function at a point, and the limit at the same point, is the same - at least for most values.


1 Answers

Assuming you expect [3, 3, 3, 3] as the answer in your question, you can use abs and zip:

[abs(i-j) for i,j in zip(list1, list2)]
like image 109
erip Avatar answered Sep 28 '22 11:09

erip