Quick one I hope.
First week back and the brain has gone fuzzy.
I am trying to count the number of list levels in a list and the number of items on those levels.
Doe’s any one have a suggestion on how to get my designscript value (the -2 circled in blue) to take a multi item list (circled in red) to effectively perform the actions circled in green?
So i need to determine how many levels the list contains before extracting the count at those levels. And I’m trying to make it dynamic, so lists of any structure/number of levels can be mapped.
List.Rank will tell you how many levels a list has (its depth). If you are dealing with jagged arrays it gets more complicated as only the deepest rank will be returned.
If you not only need to know the deepest rank but also the count of items at each level then a recursive function (Python or DS for example) would be the easiest solution.
So starting on the recursion and stumbling when I get to the same step.
I am wanting to replicate changing the list levels on the count node, multiple times for the same input list to get the different item numbers at each level.
Simple counting at some levels recursively works but what do i need to modify to add in multiple levels (Count(list@-1<1>, Count(list@-2<1> … ) Straight parameter swapping throws the same error as before. Thoughts?
Still trying to work on a way to make it work for any size list but I got a python script to work so far. I want to make a function to do the for-if-counting for me so I will keep working on it for now.
#The inputs to this node will be stored as a list in the IN variables.
theList = IN[0]
def depth(nested):
instring = False
count = 0
depthlist = []
for char in repr(nested):
if char == '"' or char == "'":
instring = not instring
elif not instring and ( char == "[" or char == ")" ):
count += 1
elif not instring and ( char == "]" or char == ")" ):
count -= 1
depthlist.append(count)
return(max(depthlist))
counts = [0]*depth(theList)
for L1 in range(len(theList)):
if type(theList[L1]) == list:
for L2 in range(len(theList[L1])):
if type(theList[L1][L2]) == list:
for L3 in range(len(theList[L1][L2])):
if type(theList[L1][L2][L3]) == list:
counting = len(theList[L1][L2][L3])
counts[3] += counting
counting = len(theList[L1][L2])
counts[2] += counting
counting = len(theList[L1])
counts[1] += counting
counting = len(theList)
counts[0] += counting
OUT = counts
Edit: I think I misunderstood the question. My method counts how many elements are at each level across all of the lists. Did you want to count how many elements are at each level categorized based on each nested list?
Using the list you have in your example: 0..3..(1..5..(1..2..(1..6)));
Can you show what you want as the output? Kind of confused as to what you want shown.
For a list of arbitrary (i.e. any) depth, you’ll need to define a recursive function.
At a certain point, iteration, such as for and while loops, stops making sense.
Yeah, that’s what I was trying to say. All I wanted to do with that was try to find what he wants as the outcome for the script, using the input he had. Then I could try writing a function to do it for any depth.
Nailed it @Dimitar_Venkov The legend strikes again!
Thanks to all others for their input to. I will put this into a worked example of what I am trying to achieve and post back here.