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.
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.
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.
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 .
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.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With