返回函数结果而不更改 if 语句中的列表项

问题描述

预先感谢您的意见。

我今天开始学习 Python,因为我想在 DynamoBIM (Revit) 中定义自定义 Python 节点,以便在 BIM 中没有预定义/适合我的任务的节点时更加灵活。

PythonScript 从输入节点获取输入,输入节点是 IN[i]
就我而言,我使用 2 个 bool 值(IN[0]IN[3])、1x str IN[1]、1x float IN[2] 和1x list IN[4]

通过 PythonScript 处理输入后,它会返回一个结果 (OUT),可用于进一步的任务。

我尝试在每个列表项前面附加一个前缀,如果 IN[0] = True 并在更改之前为每个列表项添加一个IN[2]。结果显示在watch-node中。


IN[3] = False 的情况下(列表没有被替换)我得到了想要的结果:

enter image description here

IN[3] = True 的情况下,自定义列表没有得到调整(没有添加前缀,没有添加值):

enter image description here


(集成)PythonScript代码

listing = [0,1,2,3,4,5,None,"null","","Text"]
praefix = IN[1]
add = IN[2]

if IN[3]:
    listing = IN[4]

if IN[0]:
    for x in range(len(listing)):
        if isinstance(listing[x],int):
            listing[x] = praefix + str(listing[x] + add)
        
    OUT = listing
    
else:
    
    for x in range(len(listing)):
        listing[x] = listing[x] + add

    OUT = listing

Python 代码(可在在线编译器中编译)

listing = [0,"Text"]
replacingList = [2,"Test",4] #IN[4]

boolPraefix = True #IN[0]
praefix = "Indexwert: " #IN[1]
add = 7 #IN[2]
customList = True #IN[3]
replacingList = [2,4] #IN[4]

if customList:
    listing = replacingList
    
if boolPraefix:
    for x in range(len(listing)):
        if isinstance(listing[x],int):
            listing[x] = praefix + str(listing[x] + add)

    print(listing)

else:
    
    for x in range(len(listing)):
            listing[x] = listing[x] + add

    print(listing)

我尝试使用 python 代码从在线编译器中的集成脚本中重现问题,但在这种情况下计算出预期结果:

['Indexwert: 9','Indexwert: 9','Indexwert: 10','Test','Indexwert: 11']

使用 https://www.programiz.com/python-programming/online-compiler/ 编译


预期结果应该是:

enter image description here

我目前不知道为什么在线编译器代码和集成的 PythonScript 之间会出现不同的结果。

解决方法

我以前在使用 dynamo 和 python 时遇到了一些问题,在大多数情况下,我发现最佳做法是在代码末尾只使用一次 OUT。 我拿了你的样品并修改了它,试试吧。 我添加了一个空列表,它将用作已处理列表的容器,并将其分配给输出

listing = [0,1,2,3,4,5,None,"null","","Text"]
#Empty List to use as Output
NewListing =[]
praefix = IN[1]
add = IN[2]

if IN[3]:
    listing = IN[4]

if IN[0]:
    for x in range(len(listing)):
        if isinstance(listing[x],int):
            listing[x] = praefix + str(listing[x] + add)
        
    NewListing = listing
    
else:
    
    for x in range(len(listing)):
        listing[x] = listing[x] + add

    NewListing = listing

OUT NewListing

并且不要忘记在 Dynamo 内的 Python 节点中查看您的缩进。

,

进行了一些额外的编辑,解决方案现已生效:

以下(集成)PythonScript 在用作 Dynamo 中的节点时产生预期结果:

listing = [0,"Text"]

praefix = IN[1]
add = IN[2]
custom = IN[4]

newListing = []

if IN[3]:
    listing = custom
    
if IN[3]:
    for x in range(len(custom)):
            try:
                listing[x] = int(custom[x])
            except:
                pass

if IN[0]:
    for x in range(len(listing)):
        if isinstance(listing[x],int):
            listing[x] = praefix + str(listing[x] + add)
        
    newListing = listing
    
else:
    
    for x in range(len(listing)):
        listing[x] = listing[x] + add

    newListing = listing

OUT = newListing

现在也可以为自定义列表实现结果:

enter image description here