📜 数组元素索引器
Pull one element out of a string array by index — negatives welcome
- input_array
- STRING
Once an LLM hands you an array of text (see StringArrayFormatter for the convert step), the next thing you always want to do is pick one element out of it. This node, "📜 数组元素索引器", is that pick: give it a LIST_STR and an index, get a single string back. The twist - and the reason you might prefer it over hand-rolled list nodes - is that it accepts negative indices and comes with a graceful fallback instead of a crash.
How it works
The input_array input accepts a real LIST_STR (marked forceInput, so it's a wire-in, not a widget). The index is resolved Python-style: a negative index counts from the end, so -1 gives you the last element - handy when you want "the final caption the LLM generated" without knowing how many there were. Out-of-range indices don't raise; they return your default_value (default "🚫 无效索引"). The parser is forgiving too: it accepts an actual list, a JSON string, or even a python-style string like ["a", "b"] if something upstream hands it text.
Inputs and output
input_array(LIST_STR) - where the elements come from.index(INT, default 0) - which element. 0-based; negatives count from the end.default_value(STRING) - the fallback for bad indices.- Output:
STRING- one element, or the default.
Where it fits
The classic pattern in this pack: an LLM (the Ollama or DeepSeek node) produces a list of candidate prompts, StringArrayFormatter converts it to LIST_STR, and StringArrayIndexer picks the one you want to actually run - say, index 0 for the first take. Pair index with the length output from the formatter and you can build "give me the last one" or iterate a batch of generations deterministically. Because it's a single string out, it plugs into anything that takes text: prompt encoders, note nodes, further LLM calls.
Installing it
ComfyUI Manager → search comfyUI_LLM, or:
cd ComfyUI/custom_nodes
git clone https://github.com/XieJunchen/comfyUI_LLM
# restart ComfyUI
Pure Python, no extra dependencies.
Where people get burned
- The output port is just named
STRING- easy to lose track of in a busy graph, but it is what it is. - If
input_arraycan't be parsed at all, you get the default, not an error. A wiring mistake upstream can look like "the node returns 🚫" with no stack trace, so check that the list is actually connected. - Everything becomes a string. Numbers in the list get stringified, so don't count on numeric outputs.
Small, predictable, and genuinely useful in any LLM-driven loop. If the pack's text utilities have a workhorse, this is it.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| input_array | LIST_STR | — | |
| index | INT | 0 | — |
| default_valueopt | STRING | 🚫 无效索引 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |