Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get the max distance (step) between values in python?

Tags:

python

list

Given an list of integers does exists a default method find the max distance between values?

So if I have this array

[1, 3, 5, 9, 15, 30]

The max step between the values is 15. Does the list object has a method for do that?

like image 489
Usi Usi Avatar asked Dec 06 '22 21:12

Usi Usi


1 Answers

No, list objects have no standard "adjacent differences" method or the like. However, using the pairwise function mentioned in the itertools recipes:

def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

...you can (concisely and efficiently) define

>>> max(b-a for (a,b) in pairwise([1, 3, 5, 9, 15, 30]))
15
like image 168
Frerich Raabe Avatar answered Jan 13 '23 01:01

Frerich Raabe