Calculate the difference each time a character changes in a list of values

Ok let's imagine that I have a list of values, for example:

list = [-0.23, -0.5, -0.3, -0.8, 0.3, 0.6, 0.8, -0.9, -0.4, 0.1, 0.6]

I would like to focus on this list, and when the sign changes to get the absolute difference between the maximum (minimum, if negative) of the first interval and the maximum (minimum, if negative) of the next interval.

For example, in the previous list, we would like to get this result:

[1.6, 1.7, 1.5]

The tricky part is that it should work for lists like:

list = [0.12, -0.23, 0.52, 0.2, 0.6, -0.3, 0.4]

What will be returned:

[0.35, 0.83, 0.9, 0.7]

This is difficult because some intervals have a value of 1, and I am having difficulty managing it.

How do you solve this with the least number of lines?


Here is my current code, but it is not working at the moment.

list - 6 , 6 else nan, np.array 1024 (, )

Hmax = []
for c in range(0,6):
    Hmax_tmp = []
    for i in range(len(list[c])):
        if(not np.isnan(list[c][i]).any()):
            zero_crossings = np.where(np.diff(np.sign(list[c][i])))[0]
            if(not zero_crossings[0] == 0):
                zero_crossings = [0] + zero_crossings.tolist() + [1023]
            diff = []
            for j in range(1,len(zero_crossings)-2):
                if
                diff.append(max(list[c][i][np.arange(zero_crossings[j-1],zero_crossings[j])].min(), list[c][i][np.arange(zero_crossings[j]+1,zero_crossings[j+1])].max(), key=abs) - max(list[c][i][np.arange(zero_crossings[j+1],zero_crossings[j+2])].min(), list[c][i][np.arange(zero_crossings[j+1],zero_crossings[j+2])].max(), key=abs))
            Hmax_tmp.append(np.max(diff))
        else:
            Hmax_tmp.append(np.nan)
    Hmax.append(Hmax_tmp)
+4
2

itertools.groupby. :

>>> from itertools import groupby
>>> lst = [-0.23, -0.5, -0.3, -0.8, 0.3, 0.6, 0.8, -0.9, -0.4, 0.1, 0.6] # the list
>>> minmax = [min(v) if k else max(v) for k,v in groupby(lst, lambda a: a < 0)]
>>> [abs(j-i) for i,j in zip(minmax[:-1], minmax[1:])]
[1.6, 1.7000000000000002, 1.5]

:

>>> lst2 = [0.12, -0.23, 0.52, 0.2, 0.6, -0.3, 0.4] # the list
>>> minmax = [min(v) if k else max(v) for k,v in groupby(lst2, lambda a: a < 0)]
>>> [abs(j-i) for i,j in zip(minmax[:-1], minmax[1:])]
[0.35, 0.83, 0.8999999999999999, 0.7]

, / . min/max minmax. , .

!

+8

max/min , .

def difference(nums):
  if not nums:
    return []
  pivots = []
  last_sign = nums[0] >= 0
  current = 0
  for x in nums:
    current_sign = x >= 0
    if current_sign != last_sign:
      pivots.append(current)
      current = 0
      last_sign = current_sign
    current = max(current, x) if current_sign else min(current, x)
  pivots.append(current)
  result = []
  for idx in xrange(len(pivots)):
    if idx + 1 < len(pivots):
      result.append(abs(pivots[idx] - pivots[idx + 1]))
  return result

>>> print difference([-0.23, -0.5, -0.3, -0.8, 0.3, 0.6, 0.8, -0.9, -0.4, 0.1, 0.6])
[1.6, 1.7000000000000002, 1.5]
>>> print difference([0.12, -0.23, 0.52, 0.2, 0.6, -0.3, 0.4])
[0.35, 0.83, 0.8999999999999999, 0.7]
+1

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


All Articles