1

I have a list/array(can make them arrays, doesn't matter):

array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]

I would like to create a new_array FROM array which looks like this:

new_array = [[1,2],[1],[1,2,3],[1,2],[1,2,3],[1,2,3,4,5]]

The logic I came up with was to start with a=0,b=1 and loop through array, when the value of array[a] < array[b], then a+=1,b+=1, but if the value of array[a]>=array[b], then append the value of b: Here is what I tried:

index_1 = [] # This will be the list which will have the FIRST index 
index_2 = [] # This will be the list which will have the SECOND index
a = 0
b = 1
for i in array:
    if i[a] < i[b]:
        a = a+1
        b = b+1
    elif i[a] >= i[b]:
        index_2.append(b)
        index_1.append(a)

So index_1 will have the first index and index_2 will have the second index and then, we can create the new_array by:

new_array = [(array[start:end]) for start,end in zip(index_1,index_2)]

Unfortunately, even though my idea is correct, the loop stops in if i[a]<i[b] because of IndexError: invalid index to scalar variable..

Srivatsan
  • 9,225
  • 13
  • 58
  • 83

4 Answers4

3

We can go through the array directly by the elements (more precisely by a pair of neighboring elements) simultaneously forming the result.

We initialize our result with a sublist of the 1st element of the array and then:

  • if the next element is greater than the previous one, we add it to the last sublist
  • otherwise - we add a new sublist with this element to the result.
    array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
    
    new_array = [[array[0]]] # assume the array is not empty
    for a,b in zip(array, array[1:]):
        if a<b:
            new_array[-1].append(b)
        else:
            new_array.append([b])
    
    print(new_array)
    # [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]
    

    UPD.

    Alternatively, we can implement it with indices.
    (based on this answer by @fortran)

    First, prepare indices to split the array:

    idxs_to_split = [idx for idx in range(1, len(array)) if not array[idx-1] < array[idx]]
    pairs = zip([0] + idxs_to_split, idxs_to_split + [None])
    

    Second, implement the splitting itself:

    new_array = [array[i:j] for i,j in pairs]
    
    print(new_array)
    # [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]
    
  • MaximTitarenko
    • 886
    • 4
    • 8
    2

    Here's a simple approach, just keep track of the previous, and check if it is smaller. If it is, append to sub. If it isnt, append sub to the new list, then create a new sub. Always append sub at the end.

    >>> new_array = []
    >>> array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
    >>> sub = []
    >>> prev = float('-inf')
    >>> for x in array:
    ...     if prev < x:
    ...         sub.append(x)
    ...     else:
    ...         new_array.append(sub)
    ...         sub = [x]
    ...     prev = x
    ...
    >>> new_array.append(sub)
    >>> new_array
    [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]
    
    juanpa.arrivillaga
    • 88,713
    • 10
    • 131
    • 172
    2
    def g(l):
        acc = []
        for i in l:
            if not acc:
                acc.append(i)
            else:
                if acc[-1] < i:
                    acc.append(i)
                else:
                    yield acc
                    acc = [i]
        yield acc
    

    Here's a generator version that should work for all iterables. Usage would be something like

    list(g(array))
    

    And gives the desired

    [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]
    
    Patrick Haugh
    • 59,226
    • 13
    • 88
    • 96
    0

    I simply checked for two conditions, 1. If next element == 1 and 2. If j is at last element of the list.

    array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
    newList = []
    
    j = 0
    k = 0
    while j < len(array)-1:
        if array[j+1] ==1:
            newList.append(array[k:j+1])
            k = j+1
        if  j+2==len(array):
            newList.append(array[k:j+2])
            k = j + 1
       j +=1
    print newList
    
    Bhushan Pant
    • 1,445
    • 2
    • 13
    • 29