the script is asking if one part is equal to the given string which is IN[1] then out the corresponding part as a result.
data = IN[0]
result = []
other_value = []
for item in data:
parts = item.split(":")
if parts[0].strip() == IN[1]:
n = parts[1]
result.append(n)
else:
other_value.append(parts[1])
OUT = result
I refactored it to make sure there is no leading space and to run in Python 2 (Revit 2022.1)
data = IN[0]
result = []
for outer in data:
for string in outer:
parts = string.split(" : ")
if parts[0] == IN[1]:
result.append(parts[1])
else:
result.append(None)
OUT = result
Is that any more efficient? I suppose I can use Tuneup to compare the different methods. Thanks also to @Mike.Buttery for the comprehension.
List comprehension is a little faster, according to Tuneup.
data = IN[0]
result = [parts[1] if parts[0] == IN[1] else None
for outer in data
for string in outer
for parts in [string.split(" : ")]]
OUT = result
versus
data = IN[0]
result = []
for outer in data:
for string in outer:
parts = string.split(" : ")
if parts[0] == IN[1]:
result.append(parts[1])
else:
result.append(None)
OUT = result
Edit: those Python functions are WRONG (do not use).
if you’re worried the white space before and after the segregated strings, on my original code the .strip() method will take care of that. if parts[0].strip() == IN[1]:
you can read more about in here: https://www.w3schools.com/python/ref_string_strip.asp
anyway, there’s a lot of way you can achieve what you were trying to do, this is just one way to do that. I think your original script works as well as the list comprehension by Mr. Mike.
Resulting code seems to work (even though I could never come up with it – thanks, AI):
OUT = [None] * len(IN[0])
for i, outer in enumerate(IN[0]):
for string in outer:
parts = string.split(" : ")
if parts[0] == IN[1]:
OUT[i] = parts[1]
break
We can see here that Stairs has no Area parameter so should return null. Quicker, too, because it has break.
.strip() is a useful function, and I am glad to learn about it . If we are making a generic node, we want to preserve the original data, which may contain leading and trailing spaces. Let’s hope the data don’t have an extra " : "!
OUT = [None] * len(IN[0])
for i, outer in enumerate(IN[0]):
for string in outer:
parts = string.split(" : ")
if parts[0] == IN[1]:
OUT[i] = " : ".join(parts[1:])
break
The key points:
Initialize OUT with Nones
Split string on " : "
If prefix matches, join remaining parts
Break inner loop to move to next outer item
This cleanly handles:
Initializing OUT with correct length
Splitting once on " : "
Getting full remainder string if matched
Setting OUT to result or None
By breaking after setting OUT, it avoids unnecessary further processing.