Code Block working with List

Hi!
I am trying to use the “IF” function to filter index values in an input list. I’m quite new to Dynamo. I look forward to receiving suggestions or instructions to fix it. Thanks!

Hi here is a version with nodes i am not sure if it works in all conditions

substring 08 02 2024.dyn (23.7 KB)

Sincerely
christian.stan

1 Like

The node cannot retrieve the values because the length of the text strings is insufficient ( less than the end index )

You can get around this by using a very small piece of code, with explanations. :wink:

# Inputs
input = IN[0] # strings
x = IN[1] # start index
y = IN[2] # finish index

# Output
result = []

# For each item ii inputs try getting the char between indices
for i in input:
    result.append(i[x:y+1])  
    # Extract characters between x and y (inclusive)

OUT = result
4 Likes

Here’s a solution using a conditional statement test?true:false
Dynamo Primer Shorthand

5 Likes

Thanks for your suggestion. I merged part of your node for testing. I also tried using node before using codeblock but it didn’t work.

1 Like

Thank you for your post. I searched for solutions on the forum. I find that using Python scripts makes the code much cleaner. Maybe that’s the next step on my learning curve. Do you have any suggestions for getting started with python?
Back to the firts post. If the names of elements have the form:
{Prefix}{Name}{info} with Prefix and {Info} yes or no
Eg: {R}C1; RC12; C2A, C1
3F; C5; C11_7F…
Is there any way to filter the names: C1, C12, C2A, C1…

For Dynamo specific workflows with Python have a look around the forum.

For more general code introduction take a look at something like the Harvard CS50 videos online. :wink:

When working with Strings (Text Values) you can use Regular Expressions to ‘sense’ their structure.

import re

input_list = IN[0]  # List of strings
result = []  # Output

for item in input_list:
    # Use regular expression to find the pattern of sequential numbers or letters after the initial 'C'
    match = re.search(r'C[A-Za-z0-9]+', item)
    if match:
        result.append(match.group())

OUT = result

1 Like

Excuse me for an extended option. I want to add an input “letter start”. How should I fix this code:

import re

input_list = IN[0]  # List of strings
x = IN[1] # start letter
result = []  # Output

for item in input_list:
    # Use regular expression to find the pattern of sequential numbers or letters after the initial 'C'
    match = re.search(r [x]'[A-Za-z0-9]+', item)
    if match:
        result.append(match.group())

OUT = result

import re

input_list = IN[0]  # List of strings
x = IN[1]  # start letter
result = []  # Output

for item in input_list:
    # Use regular expression to find the pattern of sequential numbers or letters after the initial 'C'
    match = re.search(r'' + re.escape(x) + r'[A-Za-z0-9]+', item)
    if match:
        result.append(match.group())

OUT = result
1 Like