Calculate the difference of factors between two list items

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!

+4
source share
5 answers

One possible way:

 list1 = [1642842,780497,1506284,1438592,1266530,1154853,965861,610252,1091847,1209404,1128111,749998] list2 = [(i, a, b, (1.0*a)/b) for (i, (a, b)) in enumerate(zip(list1, list1[1:]))] for i, a, b, f in filter(lambda (i, a, b, f): f > 1.5, list2): print 'item {0} ({1}), {2} ({3}) value {4:.1f}'.format(i, a, i+1, b, f) 

Output:

 item 0 (1642842), 1 (780497) value 2.1 item 6 (965861), 7 (610252) value 1.6 item 10 (1128111), 11 (749998) value 1.5 
+1
source

Using pairwise function from itertools recipe :

 import itertools def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b) diff = [round(abs(x/float(y)), 1) for x, y in pairwise(your_iterable)] 
+4
source
 >>> g = ((i, i + 1, round(1.0 * list1[i] / list1[i + 1], 2)) for i in range(len(list1) - 1)) >>> print [x for x in g if x[2] >= 1.5] [(0, 1, 2.1), (6, 7, 1.58), (10, 11, 1.5)] 

or

 >>> g1 = ((i, i + 1) for i in range(len(list1) - 1)) >>> g2 = ((x, y, round(float(list1[x]) / list1[y], 2)) for x, y in g1) >>> print [x for x in g2 if x[2] >= 1.5] [(0, 1, 2.1), (6, 7, 1.58), (10, 11, 1.5)] 
+1
source

Check out this solution also:

 a = [..., ..., ] for i,v in enumerate(map(lambda t: float(t[0])/float(t[1]), zip(a, a[1:]))): print "{0},{1} - {2:.1f}".format(i,i+1,v) 

Test:

 >>> a [24144, 24666, 10421, 28925, 23619, 13703, 20766, 10029, 23655, 22183] >>> for i,v in enumerate(map(lambda t: float(t[0])/float(t[1]), zip(a, a[1:]))): ... print "{0},{1} - {2:.1f}".format(i,i+1,v) ... 0,1 - 1.0 1,2 - 2.4 2,3 - 0.4 3,4 - 1.2 4,5 - 1.7 5,6 - 0.7 6,7 - 2.1 7,8 - 0.4 8,9 - 1.1 >>> 
+1
source

What about

  for i in (float(x)/y if x > y else float(y)/x for (x, y) in zip(list1[1:], list1[:-1])): print i 
0
source

Source: https://habr.com/ru/post/1494264/


All Articles