You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Autodesk Revit Dynamo Python脚本语法错误排查求助

Hey there! Let's work through your Dynamo Python script problem together. First, let's break down those syntax errors you ran into, then build a solid solution for grouping duplicate items in your list.

First, let's fix those syntax errors

The "unexpected token '<newline>'" error almost always happens when you have an incomplete conditional statement—like writing elif without following it up with a condition and colon. For example, if you had something like this:

if item == current_val:
    temp_list.append(item)
elif  # No condition here!
    result.append(temp_list)

Python gets confused because it expects a condition after elif before it hits the newline.

Even after fixing that, invalid syntax errors usually boil down to:

  • Mixed indentation (using tabs and spaces together—Python hates this; stick to 4 spaces per indent level)
  • Missing colons after if/elif/else/for/while
  • Undefined variables that you tried to use in your conditions

Now, the solution for grouping duplicate items

Based on your requirement (taking an input like (1,1,1,2,2,3,4,4,4,4,5) and outputting sublists of duplicates), here are two reliable methods tailored for Dynamo's Python environment:

Method 1: Dictionary-based grouping (works for unordered or ordered lists)

This method is foolproof regardless of the order of your input items—it will group all instances of the same value together, even if they're scattered in the input.

# Grab the input from Dynamo (IN[0] is your first input list/tuple)
input_list = list(IN[0])

# Create a dictionary to map each unique item to its list of duplicates
grouped_items = {}

for item in input_list:
    # If the item isn't in our dictionary yet, create an empty list for it
    if item not in grouped_items:
        grouped_items[item] = []
    # Add the current item to its corresponding list
    grouped_items[item].append(item)

# Convert the dictionary's values (our grouped lists) into a final list for Dynamo's OUT
OUT = list(grouped_items.values())

Testing this with your sample input will give you:
[[1, 1, 1], [2, 2], [3], [4, 4, 4, 4], [5]]

Method 2: Using itertools.groupby (best for ordered/sorted lists)

If your input list is already sorted (or you want the grouped output sorted), groupby is a more concise option. Just note that groupby only groups consecutive matching items, so we need to sort the input first if it's unordered.

# Import the groupby tool from Python's standard library
from itertools import groupby

# Get and sort the input list
input_list = list(IN[0])
sorted_list = sorted(input_list)

# Group consecutive matching items and convert to sublists
grouped_result = [list(group) for key, group in groupby(sorted_list)]

OUT = grouped_result

This will also give you the same sorted grouped output as the dictionary method.

Quick tips for Dynamo Python scripts

  • Always make sure to convert IN inputs to lists (using list()) if they come in as tuples—tuples are immutable and can cause issues if you try to modify them.
  • Double-check your indentation: Dynamo's Python editor can be finicky here, so stick to 4 spaces per indent.
  • Test small input samples first (like your (1,1,2,3) example) to debug before scaling to larger lists.

内容的提问来源于stack exchange,提问作者CHaney

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:17:06