How to use List.Transpose Method in Python Script Node Dynamo?

I’m learning to translate a dynamo script and compact it into a single python node. I need to use the List.Transpose method… how to write it in python? and what references i need to add to be able to run the method?

I already tried using ironpython List but got error no transpose method
also tried import DSCore but cannot find any documentation about the class and methods of it

the original script


#new user can’t upload dynamo script

currently stuck at this

import clr

# import Revit Services 
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("System")
from System.Collections.Generic import List

clr.AddReference('DSCoreNodes')
from DSCore import List as Dlist

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line

if hasattr(IN[0], '__iter__'):
	bbox = IN[0][0]
else:
	bbox = IN[0]

	
minpoint = bbox.MinPoint
maxpoint = bbox.MaxPoint
combinep = [minpoint,maxpoint]

Plist = List [Point]()
for i in combinep: Plist.Add(i) 
	
combinep.Transpose()
	
#Plist = List.Create(minpoint,maxpoint)



# Assign your output to the OUT variable.
OUT = bbox, 

Better to use the Python method zip than import the list.transpose method.

zip(list1,list2, list3…)

2 Likes

Thanks for the reply. Using the Python zip method for list manipulation can be a solution.

But can you explain the logic of the Dynamo List.Transpose method?
And how do I translate it into Python zip list and for loop to achieve the same result?

For an example use this problem
transpose list

A little google:

Using zip() in Python

Python’s zip() function is defined as zip(*iterables). The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

Passing

See

Using the Python zip() Function for Parallel Iteration – Real Python.

Good luck

1 Like

Hello, you have the power of DesignScript also as a possible way with a function


code block:

def info(bb:var)
{
i=BoundingBox.MinPoint(bb);
j=BoundingBox.MaxPoint(bb);
l=Line.ByStartPointEndPoint(i,j);
centroid=Autodesk.DesignScript.Geometry.Curve.PointAtParameter(l,0.5);
dx=j.X-i.X;
dy=j.Y-i.Y;
dz=j.Z-i.Z;
rep=dz==0?true:false;
_Key=["Centroid-->","Width-->","Lenght-->","Height-->","BB Flat-->"];
_Val=[centroid,dx,dy,dz,rep];
return DesignScript.Builtin.Dictionary.ByKeysValues(_Key,_Val);
};

Cordially
christian.stan

I reviewed this in the 6th Dynamo office hours, posted here: https://m.youtube.com/playlist?list=PLdlF7MirPEC2yNFTGymESd3t7Xosfk9c2

Zip basically does the same, as noted in the link above.

Thanks @jwroelofs for informing the zip(*iterables) method

Here is my code to emulate the result of the dynamo List.Translate node
But I still cannot figure out how to implement dynamo node input list level selection to affect the result
maybe @jacob.small can help with this problem

data = IN[0]

def maxlistdepth(list):
	maxdepth = 0
	for i in list:
		newdepth = len(i)
		if maxdepth < newdepth:
			maxdepth = newdepth
	return maxdepth
	
def addnull(list,maxdepth):
	for i in list:
		while len(i) < maxdepth:
			i.append(None)

maxdlist = maxlistdepth(data)
addnull(data,maxdlist)

OUT = list(zip(*data))

Another solution is to import DSCore Builtin Dynamo Library to use the List.Transpose method

import sys
import clr
clr.AddReference('DSCoreNodes')
from DSCore import List as DList

data = IN[0]
transpose = DList.Transpose(data)

OUT = transpose

The Results are the same

Thanks, @christian.stan for Design.Script solution. I learned from you that CodeBlock can be used to make functions only without any input or output and the function can be used in other CodeBlock

Hereby I attach the .dyn
Python Transpose.dyn (12.8 KB)

1 Like

This is Bering a bit off topic, but there is no need for the transpose in your graph. You want the center, width, and length of a bounding box, so…

Typing off my phone, but hopefully this makes sense if it doesn’t just work with the default inputs.

bbList = IN[0]
results = []
for bb in bbList:
    minPnt = bb.MinPoint
    maxPnt = bb.MaxPoint
    vect = Vector.ByTwoPoints(minPnt, maxPnt)
    width = vect.X
    length = vect.Y
    height = vect.Z
    cent = minPnt.Add(vect.Scale(0.5))
    data = [cent, width, length, height]
    results.append(data)
 
OUT = zip(*results) #returns the lists in order of centers, then widths, then lengths, then heights. If you want stuff groups by bounding box just use OUT = results
1 Like

10 posts were split to a new topic: List.Rank in Python