-2

This is a sample_list = [628.6, 628.25, 632.8, 634.65, 634.9, 635.85, 633.55]

I want to create a bool list of true/false based on the condition that if a list value is greater than the previous value, it should be true, else false. In my case, result should be something like this:

mybools = [False,True,True,True,True,False]

how do I do that?

KawaiKx
  • 9,558
  • 19
  • 72
  • 111

2 Answers2

0

Use list comprehension:

sample_list = [628.6, 628.25, 632.8, 634.65, 634.9, 635.85, 633.55]
bool_list = [bool(max(sample_list[i-1] - sample_list[i], 0)) 
             for i in range(1, len(sample_list))]

This uses the fact that floats can be cast to boolean values. Anyhint 0 is false, all else is True.

Galo do Leste
  • 703
  • 5
  • 13
0

Using Numpy, you can accomplish this succinctly by first computing the first-order difference of the input list with numpy.diff, and then inspecting the sign of each entry in the resulting array:

import numpy as np 

sample_list  = [628.6, 628.25, 632.8, 634.65, 634.9, 635.85, 633.55]

mybools = np.diff(sample_list) > 0

print(mybools.tolist())

outputs

[False, True, True, True, True, False]
Brian61354270
  • 8,690
  • 4
  • 21
  • 43