0

I am working in Python for the first time and I need to find an efficient way to search if a continuous sequence of three, four or five elements are the same in a larger array.

For example:

array = [1, 0, 0, 0, 1]

Output:

number_same = 3
element = 0
positions = [1, 2, 3]

Any suggestions or help?

Thx!

4 Answers4

3

The following line will give you a list of tuples of a value and its locations within the array (grouped by repetition):

from itertools import groupby
[(k, [x[0] for x in g]) for k, g in groupby(enumerate(array), lambda x: x[1])]
>>> [(1, [0]), (0, [1, 2, 3]), (1, [4])]

You can later filter it to get only repetitions of 3 and over:

filter(lambda x: len(x[1])>2, grouped_array)

Used the following answer as reference: What's the most Pythonic way to identify consecutive duplicates in a list?

Uri Hoenig
  • 138
  • 6
0

I don't know Python well but I don't imagine there's an inbuilt function to accomplish this.

you could iterate through the list and use a second array as a counter.

i.e. if the number in position 0 is a 1, then add 1 to position 1 in the second array

original_array = [1, 0, 0, 0, 1]
second_array_after_populating = [3, 2, 0, 0, 0]

then you can just scan the list once to find the most common number, and how many of that number. Once you know the number you can scan back through the original list to find the positions it appears in.

GrepGrep
  • 423
  • 1
  • 4
  • 7
0

I think that the Counter class will be useful for you.

from collections import Counter
array = [1, 0, 0, 0, 1]
counter = Counter(array)
mc = counter.most_common(20)
print(mc)

# [(0, 3), (1, 2)]
most_common = mc[0][0] #  = 0
number_same = mc[0][1] #  = 3
positions = [i for i, x in enumerate(array) if x == most_common]

The last line coming from this SO post.

The Pjot
  • 1,801
  • 1
  • 12
  • 20
  • Thanks for the suggestion, but if you would then give an array that does not have a continuous sequence, it gives still output which is not correct... – user2995603 Nov 01 '17 at 08:44
0

This is not a complete answer, but it is a start.

This uses the groupby() method associated with the itertools library. The groupby() method looks for sequential groups of values (as opposed to true groups of values) thus it is ideally suited to finding sequences.

array = [1, 0, 0, 0, 1]

from itertools import groupby

g = groupby(array)
for value, grp in g:

grp is an iterator... we can expose the contents by casting it with the list() function to extract the values into a list.

    grp = list(grp)
    length = len(grp)

The if statement using in is a convenient method to check for various values.

    if length in [3, 4, 5]:
        print('number_same =', length)
        print('element =', value)
        print('positions =', 'still working on this')

==== OUTPUT ====
number_same = 3
element = 0
positions = still working on this
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • Thanks! Seems to be working so far with both arrays that do and do not have a continuous sequence. :-) If you get anything out for the positions, I would be very grateful ;-) – user2995603 Nov 01 '17 at 08:45