【问题标题】:Result of function is returned without changing list items in if-statement返回函数结果而不更改 if 语句中的列表项
【发布时间】:2021-09-18 18:18:39
【问题描述】:

提前感谢您的意见。

我今天开始学习 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]。结果显示在观察节点中。


如果是 IN[3] = False(列表未被替换),我会得到想要的结果:

如果是 IN[3] = True,自定义列表不会得到调整(不添加前缀,不添加值):


(集成)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,1,2,3,4,5,None,"null", "", "Text"]
replacingList = [2,2,3,"Test",4] #IN[4]

boolPraefix = True #IN[0]
praefix = "Indexwert: " #IN[1]
add = 7 #IN[2]
customList = True #IN[3]
replacingList = [2,2,3,"Test",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/编译


预期结果应该是:

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

【问题讨论】:

  • 我们看不到范围。这段代码 sn-p 是函数的一部分吗?此脚本是否运行多次?如果是这样,那么它怎么称呼?请发布带有上下文和示例输入的脚本。更广为人知的是MCVE
  • 代码 sn-p 是可视化编程图表的一部分,并通过 PythonScript 处理来自输入节点 (IN[i]) 的输入。代码运行 1 次并产生返回到下一个连接节点(名称为“watch”的节点)的结果。左侧的 5 个字段提供输入,在脚本内部使用。给出的示例: praefix = IN[1] = "True" from field: "place praefix in front of list items?"
  • 我只是认为可能有一个非常明显的原因,可以很容易地检测到。我会尝试对其进行调整并将其放入 phyton 在线编译器中,这样会更透明。

标签: python revit-api bim


【解决方案1】:

我以前在使用 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 节点中查看您的缩进。

【讨论】:

  • 感谢您的意见。通过将您的代码与在 try 块中将自定义列表的项目转换为 int 相结合,我能够产生预期的结果。似乎列表值未被识别为 int 值。
【解决方案2】:

做了一些额外的编辑,解决方案现在有效:

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

listing = [0,1,2,3,4,5,None,"null", "", "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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2014-01-15
    相关资源
    最近更新 更多