1

I have a list of index's as such:

index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]

and I am given a:

pattern_width = 4
n = pattern_width
final_list = []

How would I run through the list analysing n elements at a time, in which if there is a case of all the elements are of equal values , they are appended to an empty list?

So, here as the first 4 elements are [5, 5, 5, 5], the value 5 would be appended to final_list. However, as the next 4 elements are [5, 5, 5, 6], that would not be appended.

The solutions would be [5, 16, 34, 42]

The problem I keep running into is that list index out of range.

My approach was:

for i in range(len(index_max)):
    x = index_max[i]==index_max[i+(pattern_width)
    final_list.append(x)

However, this is unable to work at the end of the list. How would I fix this? Thank you.

4 Answers4

2
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]   
n = 4
final_list = [index_max[i] for  i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1]

This should do the trick. Breaking it down,

  1. index_max[i] returns the index value if the conditions are met.
  2. range(len(index_max)-(n-1)) - this will search through every combo of 4 in the list. The n-1 will ensure that the list stops on the last combo of length 4.
  3. len(set(index_max[i:i+n])) == 1 converts the test list into a set. This will allow you to evaluate the unique values based on the length of the set.

If you were concerned about duplicate values in your list, you simply use a set comprehension as shown below,

final_list = {index_max[i] for  i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1}
W Stokvis
  • 1,409
  • 8
  • 15
1

You can try the following

index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4
final_list = []

for i in range(len(index_max) - pattern_width):
    temp = index_max[i:i + pattern_width]
    s = set(temp)
    if len(s) == 1:
        final_list.append(temp[0])

print(final_list) # Output [5, 16, 34, 42]

Working Example https://ideone.com/Rz2gbK

Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
1

Try this :

index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4

final_list = []


for i in range(len(index_max)-3):
    count = 1
    for j in range(pattern_width):
        if index_max[i] == index_max[i+j]:
            count += 1
    if count == 4:        
        final_list.append(index_max[i])
print(final_list)
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
0

Here is a solution using a bunch of methods from itertools. I went ahead and broke the problem up into 3 parts so that if you need to do other things with the code then its more flexible.

import itertools
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
n = 4

def all_equal(iterable):
    g = itertools.groupby(iterable)
    return next(g, True) and not next(g, False)

def window(iterable, window_size):
    iters = itertools.tee(iterable, window_size)
    for i in range(1, window_size):
        for it in iters[i:]:
            next(it, None)
    return zip(*iters)

def uniques_from_window(iterable, window_size):
    return [sub_list[0] for sub_list in window(iterable, window_size) if all_equal(sub_list)]

print(uniques_from_window(index_max, n))

outputs

[5, 16, 34, 42]
Grant Williams
  • 1,469
  • 1
  • 12
  • 24