Home > OS >  Extract the indices for a set of repeating data in a list
Extract the indices for a set of repeating data in a list

Time:12-11

I have a list of angles that always start with -90 to 90, but some of the data may have more or less values in between the start and end points. The list would look something like this:

Angles = [-90, -60, -30, 0, 30, 60, 90, -90, -45, 0, 45, 90, -90, -60, -45, 0, 45, 60, 90]

I have another list with corresponding values to these angles that I want to pull without having to know the indices for the entire segment since the amount of number between the -90 and 90 may change.

For example, if we call:

Angles[0:7] that should report [-90, -60, -30, 0, 30, 60, 90]

Angles[7:12] that should report [-90, -45, 0, 45, 90] and so on and so forth.

I have another list called "data" which have corresponding variables to the angles.

data = [50, 120, 0, 75, 231, 623, 30, 40, -45, 0, 3215, 955, 40, -440, -45, 210, 445, 660, 190]

Essentially I want a script that can grab the indices of all the angles and corresponding data points. It would go through the Angles variable and separate out the points from -90 to 90, then it would grab the indices (let's say Angles[0:7], Angles[7:12], etc) and would allow us to put the corresponding data points into a new variable. Segment1 = data[0:7], Segment2 = data[7:12]. I can't hardcode the exact indices since the number of points in a -90 to 90 sweep may change.

I have tried to modify one of the answers from here: Python finding repeating sequence in list of integers? but this was using multiple lists. Could not try to understand how to use it within the same list.

CodePudding user response:

Here is one way you could do it:

# create empty list to store indices
indices = []

# create empty list to store segments
segments = []

# keep track of the current segment
current_segment = []

# iterate over the angles in the Angles list
for i, angle in enumerate(Angles):
    # if the current angle is -90, start a new segment
    if angle == -90:
        current_segment = []
        indices.append(i)
    # if the current angle is 90, end the current segment
    elif angle == 90:
        current_segment.append(i)
        segments.append(current_segment)
    # otherwise, just add the index to the current segment
    else:
        current_segment.append(i)

# now you can use the indices to grab the corresponding data points
# using the indices list, you can create the segments as follows:
segment1 = data[indices[0]:indices[1]]
segment2 = data[indices[1]:indices[2]]

Here, segment1 will be a list containing the data points corresponding to the angles in the first segment of Angles, segment2 will be a list containing the data points corresponding to the angles in the second segment of Angles, and so on.

CodePudding user response:

It's a bit dirty logic but this is what can help you immediately

lsit_value = [-90, -60, -30, 0, 30, 60, 90, -90, -45, 0, 45, 90, -90, -60, -45, 0, 45, 60, 90]


def test(lsit_value):
    x = 0
    couples = []
    result = []
    for i in lsit_value:
        if i == -90 :
            couples.append(x)   
        elif i == 90:
            couples.append(x)
            result.append(list(couples))
            couples.clear()
        x = x 1
    return result


print(test(lsit_value))

result of print [[0, 6], [7, 11], [12, 18]]

  • Related