Iterate through arraylist calculating the difference of consecutive values in python -
i'm trying iterate on arraylist saving in every loop highest/lowest difference of consecutive values.
e1=([ 0 , 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39]) hodiffmax = 0 hodiffmin = 0 k in e1: diff1= e1[k+1] - e1[k] if diff1 > hodiffmax: hodiffmax=diff1 if diff1 < hodiffmin: hodiffmin=diff1
the problem "index out of bound" error. how can iterate through arraylist [k+1]? tried bunch of things dont smarter. appreciate help!
edit (that works neither):
for k in e1: w in k: diff1= e1[w+1] - e1[w] if diff1 > hodiffmax: hodiffmax=diff1 if diff1 < hodiffmin: hodiffmin=diff1
error: w in k - typeerror: 'numpy.int32' object not iterable
use grouper recipe:
def grouper(iterable, n, fillvalue=none): "collect data fixed-length chunks or blocks" # grouper('abcdefg', 3, 'x') --> abc def gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) itertools import izip_longest # required grouper = [0, 0, 0, 0, 15, 28, 28, 28, 27, 27, 35, 44, 43, 43, 42, 39] lowest = none highest = none z,q in grouper(i, 2): v = z-q if v < lowest: lowest = v if v > highest: highest = v print(lowest) print(highest)
Comments
Post a Comment