1

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 14, 2022 at 17:35
6
  • A quick question, what happens if 2 or more fields contain the word 'orange'? Commented Oct 14, 2022 at 18:16
  • The full string remains in the same row, in a new field. So no conflicts. Commented Oct 14, 2022 at 18:20
  • Your current logic is checking whether the string 'orange' is in a tuple, it isn't checking a substring 'orange' is in the contents of each item in the tuple. For example, 'orange' in 'Apples and oranges' is true while 'orange' in ('Apples and oranges',) is false. Commented Oct 14, 2022 at 18:20
  • when I run this script with just one field, !Summer!, for example, it returns the full string. I expected that logic be the same in a list of fields. Obviously, I'm wrong about that. I just dont know why... Commented Oct 14, 2022 at 18:26
  • Is there a format to specify that the items in the list are fields to search within? In the expression portion I have the exclamation marks !Summer! to designate they are field. But those dont work in the Code Block Commented Oct 14, 2022 at 18:36

1 Answer 1

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'
>>>
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Oct 14, 2022 at 19:55
1
  • 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. Commented Oct 18, 2022 at 14:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.