Use python to split a list of values into two lists when negative value occurs when looping -


let's have list of floats. wondering how loop through list , whenever negative value occurs, split list 2 separate lists.

the initial set of values: [0.1, 0.5, 3.2, 8.2, 0.0, 19.7, 0.0, -0.8, -12.0, -8.2, -2.5, -6.9, -1.3, 0.0]

example result looking for:

lista = [0.1, 0.5, 3.2, 8.2, 0.0, 19.7, 0.0]

listb = [-0.8, -12.0, -8.2, -2.5, -6.9, -1.3, 0.0]

the key here length of list vary, , position @ first negative value occurs never same.

so in short: wherever first negative value occurs, split 2 separate lists.

any ideas? appreciated. -cheers

first, may use generator expression find index of first negative value:

neg = next((i i, v in enumerate(values) if v < 0), -1) 

then, slice list (assuming neg != -1):

lista, listb = values[:neg], values[neg:] 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -