Hello wizards, I am back for more of your awesome guidance. I am attempting to split lists of lists by true values. I have python script which works for a single list, but am lost when i have multiple lists how to get the python to work. Attached image shows the AllIndicesOf values for true values. I want to ultimately chop the lists at all these values to create new sublists. I think i need a loop or a zip function possibly but i have never used one. I am hoping i made sense and that someone can shed some insight to make this work.
Why Python? And does it have to be Python?
One liner to get indexes - assumes a list of lists with true/false
indexes = [[i for i, v in enumerate(lst) if v] for lst in IN[0]]
Now to slice the lists on the index
def slicer(values, indexes):
indexes.sort()
return filter(None, [values[s:e] for s, e in zip([0]+indexes, indexes+[len(values)])])
values = IN[1]
indexes = [[i for i, v in enumerate(lst) if v] for lst in IN[0]]
OUT = [slicer(v, i) for v, i in zip(values, indexes)]
It doesnât have to be python if you have a solution without it.
Good to know.
I allways like to provide non Python solutions (IF possible) for people who
donât know Python (like me )
Is @Mike.Buttery âexampleâ what you were after?
Python makes this really easy, but you can still do it with nodes. You just need to use the true
indices to define start/stop sequences and be mindful of the list levels.
Both @Mike.Buttery and @Nick_Boyts solutions work great. I guess it just depends on whether you like python or prefer use of nodes. Thank you much gentlemen!
I was experimenting and i came up with something like this .
But i would need some sort a CumulativeSum but one which would substract.
You can essentially do that really easily by just creating a list of âshifted indicesâ by adding 0 to the beginning and subtracting.
However, flattening the list means you no longer respect the original sublists. Not sure if thatâs whatâs intended or not.
Fair point. And thanks for the example how to subtract a value with the previous.
Learned something again today (thatâs i also why i post my brain farts sometimes ).
If you have Python chops consider a yielding function, I use that for breaking down lists into chunks in this video and use this technique often when dealing with things like finding continuous periods of solar access.