I have the following question for which I cannot find the answer. My python knowledge is basic, and I'm currently working with versions 2.7.2 and 2.6.5.
Let's say I have the following list:
list1 = [1642842,780497,1506284,1438592,1266530,1154853,965861,610252,1091847,1209404,1128111,749998]
I want to know the difference of the coefficients for every 2 points (point 0.1, point 1.2, point 2.3, etc.).
The output should be like this (but preferred rounded to 1 decimal):
list2 = [2.1048665145,0.5181605859,1.047054342,1.1358530789,1.0967023509,1.195672048, 1.5827248415,0.5589171377,0.9027975763,1.0720611713,1.5041520111]
The end result I am looking for is that when the coefficient is greater than 1.5, I want to report two numbers of the list numbers and their coefficient value.
position 0.1 value 2.1
position 6.7 value 1.6
position 10.11 value 1.5
How should I do it?
You can easily find the numerical difference with:
print numpy.diff(list1)
or
for i in (abs(x - y) for (x, y) in zip(list1[1:], list1[:-1])): print i
But am I racking my brains to find a solution to my question above? I tried several things with the above code, but I can not get a good result.
Also note that although I will first filter the data in list1, it will contain upper null values โโthat used to lead to dividebyzero problems.
EDIT: Thanks for the solutions, most of them do exactly what I want. Unfortunately, the items in these lists have a fixed position. This information, I canโt refuse, therefore filtering some elements from the list to prevent errors, such as "ZeroDivisionError: float division by zero", is not really an option. To explain a little more, there will most likely be lists of the following format:
list1 = [0,0,0,0,0,0,0,0,2,5,65,456,456456,456564,456666,666666,2344,233,232,122,88,6,0,0,0,0]
What is pythonic with any of the solutions below to solve this problem? To be more specific regarding the output:
position 0.1 value 0
position 1.2 value 0
item 2,3 value 0
and etc.
position 8.9 value 2.5
position 9.10 value 13
and etc.
Last edit: Iโm filtering the data anyway, and Iโm not creating problems for correction. Thanks everyone for the answers!