Finding the largest delta between two integers in a list - python

I have a list of integers, i.e.:
values = [55, 55, 56, 57, 57, 57, 57, 62, 63, 64, 79, 80]
I am trying to find the largest difference between two consecutive numbers.
In this case it would be 15 from 64->79.
The numbers can be negative or positive, increasing or decreasing or both. The important thing is I need to find the largest delta between two consecutive numbers.
What is the fastest way to do this? These lists can contain anywhere from hundreds to thousands of integers.
This is the code I have right now:
prev_value = values[0]
largest_delta = 0
for value in values:
delta = value - prev_value
if delta > largest_delta:
largest_delta = delta
prev_value = value
return largest_delta
Is there a faster way to do this? It takes a while.

max(abs(x - y) for (x, y) in zip(values[1:], values[:-1]))

Try timing some of these with the timeit module:
>>> values = [55, 55, 56, 57, 57, 57, 57, 62, 63, 64, 79, 80]
>>> max(values[i+1] - values[i] for i in xrange(0, len(values) - 1))
15
>>> max(v1 - v0 for v0, v1 in zip(values[:-1], values[1:]))
15
>>> from itertools import izip, islice
>>> max(v1 - v0 for v0, v1 in izip(values[:-1], values[1:]))
15
>>> max(v1 - v0 for v0, v1 in izip(values, islice(values,1,None)))
15
>>>

This is more as an advertisement for the brilliant recipes in the Python itertools help.
In this case use pairwise as shown in the help linked above.
from itertools import tee, izip
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
values = [55, 55, 56, 57, 57, 57, 57, 62, 63, 64, 79, 80]
print max(b - a for a,b in pairwise(values))

With reduce (ugly i guess)
>>> foo = [5, 5, 5, 5, 8, 8, 9]
>>> print reduce(lambda x, y: (max(x[0], y - x[1]), y), foo, (0, foo[0]))[0]
3

Starting in Python 3.10, the new pairwise function provides a way to slide through pairs of consecutive elements, and thus find each of their differences:
from itertools import pairwise
# values = [55, 55, 56, 57, 57, 57, 57, 62, 63, 64, 79, 80]
max(abs(x-y) for x, y in pairwise(values))
# 15
The intermediate result of pairwise:
pairwise([55, 55, 56, 57, 57, 57, 57, 62, 63, 64, 79, 80])
# [(55, 55), (55, 56), (56, 57), (57, 57), (57, 57), (57, 57), ...]

Related

in python language create several variables in a loop and after append items in each ones

I'm trying to find a way to append items in variables created on the fly
c = ('a','b','g','d','j')
p = 2
for r in c:
globals()['ssvar%s' % r] = []
for z in range (0,10,1):
for r in c:
p = p + 2
(['ssvar%s' % r]).append (p)
print ssvara #result >>> []
print ssvarb #result >>> []
print ssvarg #result >>> []
print ssvard #result >>> []
print ssvarj #result >>> []
but the expression (['ssvar%s' % poire]).append doesn't work.
can you direct me to the same topic or tell me how to vary the variable name to be fill ?
Don't do this, but I think what you were looking to do is
c = ('a','b','g','d','j')
p = 2
for r in c:
globals()['ssvar%s' % r] = []
for z in range (0, 10, 1):
for r in c:
p = p + 2
globals()['ssvar%s' % r].append(p)
Instead, you can create your own dictionary (container of key: value pairs) and store the lists in there as values and use the keys as names. If this dictionary is called my_dict, then my_dict['ssvara'] references the list corresponding to 'ssvara', my_dict['ssvarb'] references the list corresponding to 'ssvarb' and so on.
c = ('a','b','g','d','j')
p = 2
my_dict = {}
for r in c:
my_dict['ssvar%s' % r] = []
for z in range (0, 10, 1):
for r in c:
p = p + 2
my_dict['ssvar%s' % r].append(p)
print my_dict
Output
{'ssvara': [4, 14, 24, 34, 44, 54, 64, 74, 84, 94],
'ssvarb': [6, 16, 26, 36, 46, 56, 66, 76, 86, 96],
'ssvard': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
'ssvarg': [8, 18, 28, 38, 48, 58, 68, 78, 88, 98],
'ssvarj': [12, 22, 32, 42, 52, 62, 72, 82, 92, 102]}
If the actual names are not important (you are, after all, creating them dynamically), you can just create a list of lists. If this list is called my_list, my_list[0] references the first sublist, my_list[1] references the second, and so on.
c = ('a','b','g','d','j')
p = 2
my_list = [[] for i in range(len(c))]
for z in range (0, 10, 1):
for l in my_list:
p = p + 2
l.append(p)
print my_list
Output
[[4, 14, 24, 34, 44, 54, 64, 74, 84, 94],
[6, 16, 26, 36, 46, 56, 66, 76, 86, 96],
[8, 18, 28, 38, 48, 58, 68, 78, 88, 98],
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
[12, 22, 32, 42, 52, 62, 72, 82, 92, 102]]
I don't use Python 2 so I had to make a few interpolations.
I try to create temporaly variables with multiple items inside.
in fact I need to have, for exemple, this result:
ssvara
>>>> ['4','6','8','10','12','14','16', '8','20','22']
ssvara
>>>> ['24','26','28','30','32','34','36', '38','40','42']
the value of p is not important, the main thing is to be able to append. even with a local variable.

what is the meaning of X[i,] in python [duplicate]

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is
p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0])))
Now, I know that when slicing an array, the first number is start, the next is end, and the last is step, but what does the comma after the end number designate? Thanks.
It is being used to extract a specific column from a 2D array.
So your example would extract column 0 (the first column) from the first 2048 rows (0 to 2047). Note however that this syntax will only work for numpy arrays and not general python lists.
Empirically - create an array using numpy
m = np.fromfunction(lambda i, j: (i +1)* 10 + j + 1, (9, 4), dtype=int)
which assigns an array like below to m
array(
[[11, 12, 13, 14],
[21, 22, 23, 24],
[31, 32, 33, 34],
[41, 42, 43, 44],
[51, 52, 53, 54],
[61, 62, 63, 64],
[71, 72, 73, 74],
[81, 82, 83, 84],
[91, 92, 93, 94]])
Now for the slice
m[:,0]
giving us
array([11, 21, 31, 41, 51, 61, 71, 81, 91])
I may have misinterpreted Khan Academy (so take with grain of salt):
In linear algebra terms, m[:,n] is taking the nth column vector of
the matrix m
See Abhranil's note how this specific interpretation only applies to numpy
It slices with a tuple. What exactly the tuple means depends on the object being sliced. In NumPy arrays, it performs a m-dimensional slice on a n-dimensional array.
>>> class C(object):
... def __getitem__(self, val):
... print val
...
>>> c = C()
>>> c[1:2,3:4]
(slice(1, 2, None), slice(3, 4, None))
>>> c[5:6,7]
(slice(5, 6, None), 7)

How to get all values possible from a list of number without itertools

I want to create an algorithm that find all values that can be created with the 4 basic operations + - * / from a list of number n, where 2 <= len(l) <= 6 and n >= 1
All numbers must be integers.
I have seen a lot of similar topics but I don't want to use the itertool method, I want to understand why my recursive program doesn't work
I tried to make a costly recursive program that makes an exhaustive search of all the possible combinations, like a tree with n=len(l) start and each tree depth is n.
L list of the starting number
C the current value
M the list of all possible values
My code:
def result(L,C,M):
if len(L)>0:
for i in range(len(L)) :
a=L[i]
if C>=a:
l=deepcopy(L)
l.remove(a)
m=[] # new current values
#+
m.append(C+a)
# * 1 is useless
if C !=1 or a !=1:
m.append(C*a)
# must be integer
if C%a==0 and a<=C: # a can't be ==0
m.append(C//a)
#0 is useless
if C!=a:
m.append(C-a)
for r in m: #update all values possible
if r not in M:
M.append(r)
for r in m: # call the fucntion again with new current values,and updated list of remaining number
result(l,r,M)
def values_possible(L) :
m=[]
for i in L:
l=deepcopy(L)
l.remove(i)
result(l,i,m)
m.sort()
return m
For small lists without duplicate numbers, my algorithm seems to work but with lists like [1,1,2,2,4,5] it misses some values.
It returns:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 94, 95, 96, 97, 98, 99, 100, 101,
102, 104, 105, 110, 112, 115, 116, 118, 119, 120, 121, 122, 124, 125, 128, 130,
140, 160]
but it misses 93,108,114,117,123,126,132,135,150,180.
Let's take an even simpler example: [1, 1, 2, 2].
One of the numbers your algorithm can't find is 9 = (1 + 2) * (1 + 2).
Your algorithm simply cannot come up with this computation because it always deals with a "current" value C. You can start with C = 1 + 2, but you cannot find the next 1 + 2 because it has to be constructed separately.
So your recursion will have to do at least some kind of partitioning into two groups, finding all the answers for those and then doing combining them.
Something like this could work:
def partitions(L):
if not L:
yield ([], [])
else:
for l, r in partitions(L[1:]):
yield [L[0]] + l, r
yield l, [L[0]] + r
def values_possible(L):
if len(L) == 1:
return L
results = set()
for a, b in partitions(L):
if not a or not b:
continue
for va in values_possible(a):
for vb in values_possible(b):
results.add(va + vb)
results.add(va * vb)
if va > vb:
results.add(va - vb)
if va % vb == 0:
results.add(va // vb)
return results
Not too efficient though.

How work unpacking in python? [duplicate]

Is it possible to simulate extended tuple unpacking in Python 2?
Specifically, I have a for loop:
for a, b, c in mylist:
which works fine when mylist is a list of tuples of size three. I want the same for loop to work if I pass in a list of size four.
I think I will end up using named tuples, but I was wondering if there is an easy way to write:
for a, b, c, *d in mylist:
so that d eats up any extra members.
You can't do that directly, but it isn't terribly difficult to write a utility function to do this:
>>> def unpack_list(a, b, c, *d):
... return a, b, c, d
...
>>> unpack_list(*range(100))
(0, 1, 2, (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99))
You could apply it to your for loop like this:
for sub_list in mylist:
a, b, c, d = unpack_list(*sub_list)
You could define a wrapper function that converts your list to a four tuple. For example:
def wrapper(thelist):
for item in thelist:
yield(item[0], item[1], item[2], item[3:])
mylist = [(1,2,3,4), (5,6,7,8)]
for a, b, c, d in wrapper(mylist):
print a, b, c, d
The code prints:
1 2 3 (4,)
5 6 7 (8,)
For the heck of it, generalized to unpack any number of elements:
lst = [(1, 2, 3, 4, 5), (6, 7, 8), (9, 10, 11, 12)]
def unpack(seq, n=2):
for row in seq:
yield [e for e in row[:n]] + [row[n:]]
for a, rest in unpack(lst, 1):
pass
for a, b, rest in unpack(lst, 2):
pass
for a, b, c, rest in unpack(lst, 3):
pass
You can write a very basic function that has exactly the same functionality as the python3 extended unpack. Slightly verbose for legibility. Note that 'rest' is the position of where the asterisk would be (starting with first position 1, not 0)
def extended_unpack(seq, n=3, rest=3):
res = []; cur = 0
lrest = len(seq) - (n - 1) # length of 'rest' of sequence
while (cur < len(seq)):
if (cur != rest): # if I am not where I should leave the rest
res.append(seq[cur]) # append current element to result
else: # if I need to leave the rest
res.append(seq[cur : lrest + cur]) # leave the rest
cur = cur + lrest - 1 # current index movded to include rest
cur = cur + 1 # update current position
return(res)
Python 3 solution for those that landed here via an web search:
You can use itertools.zip_longest, like this:
from itertools import zip_longest
max_params = 4
lst = [1, 2, 3, 4]
a, b, c, d = next(zip(*zip_longest(lst, range(max_params))))
print(f'{a}, {b}, {c}, {d}') # 1, 2, 3, 4
lst = [1, 2, 3]
a, b, c, d = next(zip(*zip_longest(lst, range(max_params))))
print(f'{a}, {b}, {c}, {d}') # 1, 2, 3, None
For Python 2.x you can follow this answer.

Grouping a list of integers with nearest values

I have a list:
d = [23, 67, 110, 25, 69, 24, 102, 109]
how can I group nearest values with a dynamic gap, and create a tuple like this, what is the fastest method? :
[(23, 24, 25), (67, 69), (102, 109, 110)]
Like
d = [23,67,110,25,69,24,102,109]
d.sort()
diff = [y - x for x, y in zip(*[iter(d)] * 2)]
avg = sum(diff) / len(diff)
m = [[d[0]]]
for x in d[1:]:
if x - m[-1][0] < avg:
m[-1].append(x)
else:
m.append([x])
print m
## [[23, 24, 25], [67, 69], [102, 109, 110]]
Fist we calculate an average difference between sequential elements and then group together elements whose difference is less than average.

Categories

Resources