Emre
Emre

Reputation: 1

How can I create sublists in Python when slope changes? I want to output the segments when direction of the value changes

For example, I have the list below:

y = [-0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.08, -0.08, -0.085, -0.085, -0.075, -0.07, -0.07, -0.065, -0.055, -0.05, -0.05, -0.04, -0.04, -0.055, -0.075, -0.08, -0.085, -0.07, -0.08, -0.08, -0.09, -0.095, -0.09, -0.095, -0.11, -0.12, -0.145, -0.135, -0.105, -0.095, -0.105, -0.12, -0.135, -0.125, -0.125, -0.115, -0.115, -0.125, -0.14, -0.14, -0.13, -0.125, -0.14, -0.145, -0.15, -0.15, -0.13, -0.115, -0.125, -0.135, -0.16, -0.195, -0.2, -0.205, -0.225, -0.255, -0.3, -0.29, -0.235, -0.13, -0.035, 0.05, 0.12, 0.2, 0.31, 0.435, 0.535, 0.58, 0.475, 0.21, -0.085, -0.23, -0.25, -0.215, -0.18, -0.185, -0.16, -0.15, -0.15, -0.18, -0.18, -0.16, -0.165, -0.17, -0.165, -0.185, -0.185, -0.185, -0.17, -0.16, -0.155, -0.175]

I want to cut the original list to create sublists. [[-0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.065, -0.08, -0.08], [-0.08, -0.085, -0.085], [-0.085, -0.075, -0.07, -0.07, -0.065, -0.055, -0.05, -0.05, -0.04, -0.04], [-0.04, -0.055, -0.075, -0.08, -0.085], ....] so on..

I created indexes to make them tuples. I thought it would be easier to cut it using the points when slope direction changes. I tried to loop through the list to slice segments out but I couldn't get any results. I was not able to do it.

[(0, -0.065), (1, -0.065), (2, -0.065), (3, -0.065), (4, -0.065), (5, -0.065), (6, -0.065), (7, -0.065), (8, -0.08), (9, -0.08), (10, -0.085), (11, -0.085), (12, -0.075), (13, -0.07), (14, -0.07), (15, -0.065), (16, -0.055), (17, -0.05), (18, -0.05), (19, -0.04), (20, -0.04), (21, -0.055), (22, -0.075), (23, -0.08), (24, -0.085), (25, -0.07), (26, -0.08), (27, -0.08), (28, -0.09), (29, -0.095), (30, -0.09), (31, -0.095), (32, -0.11), (33, -0.12), (34, -0.145), (35, -0.135), (36, -0.105), (37, -0.095), (38, -0.105), (39, -0.12), (40, -0.135), (41, -0.125), (42, -0.125), (43, -0.115), (44, -0.115), (45, -0.125), (46, -0.14), (47, -0.14), (48, -0.13), (49, -0.125), (50, -0.14), (51, -0.145), (52, -0.15), (53, -0.15), (54, -0.13), (55, -0.115), (56, -0.125), (57, -0.135), (58, -0.16), (59, -0.195), (60, -0.2), (61, -0.205), (62, -0.225), (63, -0.255), (64, -0.3), (65, -0.29), (66, -0.235), (67, -0.13), (68, -0.035), (69, 0.05), (70, 0.12), (71, 0.2), (72, 0.31), (73, 0.435), (74, 0.535), (75, 0.58), (76, 0.475), (77, 0.21), (78, -0.085), (79, -0.23), (80, -0.25), (81, -0.215), (82, -0.18), (83, -0.185), (84, -0.16), (85, -0.15), (86, -0.15), (87, -0.18), (88, -0.18), (89, -0.16), (90, -0.165), (91, -0.17), (92, -0.165), (93, -0.185), (94, -0.185), (95, -0.185), (96, -0.17), (97, -0.16), (98, -0.155), (99, -0.175)]

Please advise.

Upvotes: 0

Views: 57

Answers (1)

Michael Hofmann
Michael Hofmann

Reputation: 124

If you want to create a new list when the direction changes i would try this code:

def create_sublists_when_direction_changes(your_list):
    new_list = False
    big_list = []
    temp_list = []
    for i in range(0,len(your_list)):
        if new_list == False:
            temp_list.append(your_list[i])
        else:
            big_list.append(temp_list)
            temp_list = [your_list[i]]
        if your_list[i] > 0:
            direction = 'positive'
        else:
            direction = 'negative'
        try:
            if your_list[i+1] > 0:
                next_direction = 'positive'
            else:
                next_direction = 'negative'
            new_list = False
            if direction != next_direction:
                new_list = True
        except:
            pass
    big_list.append(temp_list)
    return big_list

It is not a real solution for your problem but a working workaround.

In my programm i counted the zero as negative, so if you want it to be treated like a positive number just change the line if your_list[i] > 0: to if your_list[i] >= 0:. Of course with the line if your_list[i+1] > 0 too.

Upvotes: 0

Related Questions