2

I have a nested list of numbers. Like this one:

[[1,2,3],[3,2,1],[3,1,2],[2,3,1]]

Now, I want to count the number(s) that is bigger than it's previous element and it's front element, it must also has to have an element on both of it's sides. The output should be like this(inside a list):

[0,0,0,1]

The first element of the output list is zero because in the first nested list, it's first element has no other previous element but only 2 in it's front. For the second element(still on the first nested list) it has 1 as it's previous element and 3 as it's front one but the output is zero because it's needs to be greater than both the element in it's front and it's previous one. The third element is is also not valid because it doesn't have a front element, only it's previous one. The same is for the other nested lists except for the last one. In the last nested list 2 and 1 is already not valid, but 3 is, so now we need to check whether if this is higher than it's two elements. Since three is higher, we count +1 for the last nested list.

Another example:

[[1,2,1,3,1],[2,2,2],[2,3,4,3,4,2,3,1]]

The output should be:

[2,0,3]

The first element of the output list is 2 because in the first nested list(the first and last element is already invalid), 2 has both elements and it is also higher than the both. Then, we move on to 1, it also has both elements but it is smaller than the both so it's invalid. 3 is valid because it has both elements and it is also higher. So we count +2 since there are 2 numbers that valid. The second nested list is invalid because the number that has both elements cannot be equal to any of them, it needs to be higher, and the first and last element is already invalid.In the last nested list, the first and last element of it is already invalid, so we start from it's second one, 3, 3 has both elements but it is only higher than it's previous one but is smaller than it's front one. Next, 4 has both elements and it is also higher so it is counted. Next, 3 has both elements but it is smaller than both of them so it's not counted. Next, 4 has both elements and it is also higher than it's elements. Next, 2 has both elements but it is smaller than it's elements so it's not counted. Next, 3 has both elements and it is also higher than the both, so it's also count. Which makes the last element in the output list count +3.

I am sorry if the explanation is too long/lengthy, but is there any way of doing this in Python 3?

Edit:

So far, I have tried this:

listy = [[1,2,3],[3,2,1],[3,1,2],[2,3,1]]
list_hold = []
for x in listy:
    for y in x:
        if y>y+1 and y>y-1:
            list_hold.append(x)
print(list_hold)

But it only return an empty list.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
Mahir Islam
  • 1,941
  • 2
  • 12
  • 33

2 Answers2

1

You need a sliding window iterator; something that produces 3 elements in a row from an input sequence; all you then have to do is count the number of such windows where the middle element is larger than the two other elements.

The sliding window problem is already solved: Rolling or sliding window iterator?, using the window() function from the top answer there would then give you:

def count_maxima(l):
    # b, the middle value, is larger than both a and c
    return sum(a < b > c for a, b, c in window(l, 3))

result = [count_maxima(sublist) for sublist in outerlist]

Demo:

>>> outerlist = [[1,2,3],[3,2,1],[3,1,2],[2,3,1]]
>>> [count_maxima(sublist) for sublist in outerlist]
[0, 0, 0, 1]
>>> outerlist = [[1,2,1,3,1],[2,2,2],[2,3,4,3,4,2,3,1]]
>>> [count_maxima(sublist) for sublist in outerlist]
[2, 0, 3]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

My first thought would be to use np.diff. If you have a given list of values, vals, you can use np.diff(vals) to get an array that is negative if the next number is smaller, and positive if the next number is larger

def count_local_maxima(vals):
    if len(vals) < 3:
        return 0
    diffs = np.diff(vals)
    return np.sum((diffs[:-1]>0) & (diffs[1:]<0))

result = [count_local_maxima(sublist) for sublist in mainlist]
David L
  • 441
  • 3
  • 9