Counting List Levels and Items at those Levels


#1

Hi All,

Quick one I hope. :grinning:
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?

A straight input swap to try and add items from another list gives this error.

image

There is probably a super simple python fix for this… but thought Id try DS first :wink:
Any help is appreciated.


#2

Not sure what you’re looking for in the end, but maybe this is the code you’re looking for?

Count(a@@-2<1L>);


#3

@Ewan_Opie Alternately try List.Count(a<1><2>) ;


#4

Thanks for the responses @JacobSmall and @Vikram_Subbaiah

I am looking to map the structure of the list.

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.

Im trying a sort of fractal mapping… :thinking:


#5

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.


#6

Thanks @Thomas_Mahon
I thought as much, no simple substitutes here.
I’ll mull it over this weekend and get back into it Monday. :+1:t2:


#7

Might be a wasted thought, but is there any way of inputting strings to “write” code into code blocks?


#8

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?


#9

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?


#10

Yes, to the later. Thanka for looking into this, great python example. :+1:t2:


#11

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.


Here is a way to get each one using OOTB nodes, just don’t know how you want to combine them.


#12

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.


#13

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.


#14

This structure of list would be the end result.

Function would be:

  1. Find the number of levels in the list (automate d function)
  2. For each of those levels count the number of items (tricky part)
  3. Compile that to a single list (no other user input)

Is that clearer?


#15


#16

Nailed it @Dimitar_Venkov :+1: 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. :grinning: