Ad multiple sublist together with multiple requirements

clockwork
excel
revit
dynamo

#1

Hello everybody,

This is my first time posting here because i cann’t find a answer for my dynamo script.
what i want to do is to sum sublist 1 when every item in sublist 0 is the same.

so when i have got the list like this:

0 list
– 0 list
----- A
----- B
----- C
–1 list
----- 1
----- 1
----- 1
----- 1
1 list
– 0 list
----- D
----- E
----- F
–1 list
----- 1
----- 1
----- 1
----- 1
2 list
– 0 list
----- A
----- B
----- C
–1 list
----- 1
----- 1
----- 1
----- 1

it will create this:

0 list
– 0 list
----- A
----- B
----- C
–1 list
----- 2
----- 2
----- 2
----- 2
1 list
– 0 list
----- D
----- E
----- F
–1 list
----- 1
----- 1
----- 1
----- 1

I hope you guys understand what i mean

With kind regards,
J. Wagemaker


#2

Hey, not very elegant, but try this maybe? @kennyb6 is great at this stuff :slight_smile:

For me, the list transpose and group by key are the important bits… I couldn’t do the grouping with Key and Value both being lists of lists (dictionaries also failed me, I’m sure @JacobSmall will point out an obvious way :P)

SumSubLists.dyn (14.5 KB)

Hope that helps,

Mark


#3

@Mark.Ackerley’s solution works quite well by the looks of it, but I interpret the question differently.

I’d create a string from the original array, an use that to sum the lists, and then do a List.GroupByKey, followed by Math.Sum and a List.Create. A better solution might be to use a dictionary though.


#4

This can probably be optimized more but it works (and without python @JacobSmall :wink:):

Should be all OOTB nodes.

This is using @JacobSmall’s method from above:

The only reason I don’t like this method is that if the keys aren’t strings, you will need some way to convert to a string. For numbers, this is easy but if it is elements or something, you will need to get its Id, which is just more pain on top of it.


#5

I actually thought String.FromArray as if it was ACB it wouldn’t want to add any of the sums. Could be misunderstanding though.


#6

String from array doesn’t allow list levels, so it would turn the whole thing into a single string. I tried it too :frowning: Would need to use List.Map but I personally dislike using it lol


#7

I got it working!
Thanks a lot Mark, Jacob and Kenny.