I am attempting a field calculation that searches multiple fields for a partial string, but returns the entire string of the field containing the partial string. For example, the partial string 'orange' could be contained in any of the fields (!Spring!, !Summer!, !Fall!, !Winter!). I need to search all of these fields containing a reference to 'orange, and return the entire string to a new field.
The problem I am having is that the Code Block does not like my syntax for searching a range of fields. The script runs, but returns NULLs into the new field. If I use this same Code Block on just one field at a time it works perfectly. But adding more than one field to the search range produced NULLs.
Also, when I attempt this with multiple fields it runs very fast. When I run it to just search one field at a time, it produces the expected output, but the process takes much longer.
See below for what is not working:
Expression:
whr(!Spring!, !Summer!, !Fall!, !Winter!)
Code Block:
def whr(Spring, Summer, Fall, Winter):
if ('orange' in (Spring, Summer, Fall, Winter)):
return (Spring, Summer, Fall, Winter)
else:
return None
1 Answer 1
The current code is checking for membership in a tuple and not for a substring in a string. If you have a tuple of strings, you need to iterate over the strings to look for a substring within them. Several ways to go about such a task, below is one of the most basic:
>>> spring_field = "April showers bring May flowers."
>>> summer_field = "Make hay while the sun shines"
>>> fall_field = "Apple doesn't fall far from the tree"
>>>
>>> def whr(spring, summer, fall):
... for season in (spring, summer, fall):
... if 'hay' in season:
... return season
...
...
>>> whr(spring_field, summer_field, fall_field)
'Make hay while the sun shines'
>>>
-
Brilliant! Thank you! This is the best solution. I got stuck for far too long trying other methods using arcp.da.searchcurser, but it was not creating the results I need. This iterator works better.rpenman– rpenman2022年10月18日 14:44:30 +00:00Commented Oct 18, 2022 at 14:44
Explore related questions
See similar questions with these tags.
'orange' in 'Apples and oranges'
is true while'orange' in ('Apples and oranges',)
is false.