Home > database >  Split list with multiple list according with a specific element
Split list with multiple list according with a specific element

Time:11-27

I have a list of coordinates that are generated by an android app. What I want is to automatically split the list every time it finds the element (0.0,0.0) into another list and when it finds the next (0.0,0.0) it will split that into another list and so on. The number of elements (0.0,0.0) depends on how many times I run the app, so in the example, I ran the app 3 times but if I had run 4 I would need to split the big_list into 4 lists. With that, the size of the big_list and the number of (0.0,0.0) elements that determine the number of splits depends on how many times I ran the app before I export the file from the database and load it on python to do the analysis.

Here's the example:

big_list = [**(0.0, 0.0)**, (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564), **(0.0, 0.0)**, (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066), **(0.0, 0.0)**, (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]

And what I want is 3 lists starting with the element (0.0,0.0) and the following elements until finds the next (0.0,0.0) to split again:

list1 = [(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564)]

list2= [(0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066)]

list3 = [(0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]

CodePudding user response:

this method will give you an answer you needed

def split_list(input_list:list) -> list:
    start_ind=0
    result = []
    for ind, x in enumerate(input_list):
        if ( x == (0.0, 0.0) or ind == len(input_list)-1 ) and ind != 0:
            result.append(input_list[start_ind:ind])
            start_ind=ind
    return result

CodePudding user response:

I agree with @conol answer but you can just add this extra step and get the result you want.

basically, we are splitting list on delimiter you have written then we are updating locale with code so you will get dynamically created variable at runtime

def split_list(input_list:list) -> list:
    start_ind=0
    dict = {}
    varname= 'list'
    counter = 0
    for ind, x in enumerate(input_list):
        if ( x == (0.0, 0.0) or ind == len(input_list)-1 ) and ind != 0:
            dict[varname str(counter)] = input_list[start_ind:ind]
            start_ind=ind
            counter =1
    return dict

locals().update(split_list(a))
print(list0,list1,list2)

CodePudding user response:

For example, a function can be defined that loops over the entries of the big list and creates the sub-lists (with type annotations):

from pprint import pprint
from typing import List, Optional, Tuple


def split_big_list(
        big_list: List[Tuple[float, float]],
) -> List[List[Tuple[float, float]]]:
    result: List[List[Tuple[float, float]]] = []
    current_list: Optional[List[Tuple[float, float]]] = None
    for entry in big_list:
        if entry == (0.0, 0.0):
            current_list = [entry]
            result.append(current_list)
        elif current_list is None:
            # ignore entries before first tuple with zeros
            continue
        else:
            current_list.append(entry)

    return result


BIG_LIST = [(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564), (0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066), (0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]
lists = split_big_list(BIG_LIST)
pprint(lists)

Result:

[[(0.0, 0.0),
  (0.7061503529548645, -0.5579889416694641),
  (1.412300705909729, -1.1159778833389282),
  (2.1184511184692383, -1.673966884613037),
  (2.824601411819458, -2.2319557666778564)],
 [(0.0, 0.0),
  (0.6936703324317932, -0.573429524898529),
  (1.3873406648635864, -1.146859049797058),
  (2.0810110569000244, -1.7202885150909424),
  (2.7677958011627197, -2.3019471168518066)],
 [(0.0, 0.0),
  (0.6973708868026733, -0.5689234137535095),
  (1.3947417736053467, -1.137846827507019),
  (2.0921125411987305, -1.7067701816558838),
  (2.7894835472106934, -2.275693655014038),
  (3.4868545532226562, -2.8446171283721924),
  (4.184225559234619, -3.4135406017303467)]]

CodePudding user response:

Use enumerate to find the indexes of the (0,0) items. Then zip() to pair them up with teir successor to form sub ranges:

starts   = [i for i,v in enumerate(big_list) if v == (0,0)]
sublists = [big_list[s:e] for s,e in zip(starts,starts[1:] [None])]

print(sublists)
[[(0.0, 0.0), (0.7061503529548645, -0.5579889416694641), (1.412300705909729, -1.1159778833389282), (2.1184511184692383, -1.673966884613037), (2.824601411819458, -2.2319557666778564)], 
 [(0.0, 0.0), (0.6936703324317932, -0.573429524898529), (1.3873406648635864, -1.146859049797058), (2.0810110569000244, -1.7202885150909424), (2.7677958011627197, -2.3019471168518066)], 
 [(0.0, 0.0), (0.6973708868026733, -0.5689234137535095), (1.3947417736053467, -1.137846827507019), (2.0921125411987305, -1.7067701816558838), (2.7894835472106934, -2.275693655014038), (3.4868545532226562, -2.8446171283721924), (4.184225559234619, -3.4135406017303467)]]


# if you want them in variables 
# (but then you need to know how many subslits there are ahead of time):

lst1,lst2,lst3 = sublists
  • Related