<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help with ListFields and CalculateField_management syntax in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214991#M16580</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Ok, this has got under my skin a little&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Don't you hate when that happens? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll give this a go as well.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 29 Aug 2012 17:47:37 GMT</pubDate>
    <dc:creator>JohnLay</dc:creator>
    <dc:date>2012-08-29T17:47:37Z</dc:date>
    <item>
      <title>Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214982#M16571</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need some assistance with the syntax for a CalculateField_management calculation as I have still yet to master all the python syntax rules.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I am trying to do is find the max value in a variable number of fields in order to populate another field. I am using the ListFields function to discover the desired fields to choose from, but getting that list into the formula is giving me some difficulty.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, os, string LAYERS = arcpy.GetParameterAsText(0) SLOSHFILEDS = [f.name for f in arcpy.ListFields(LAYERS,"","DOUBLE") arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(SLOSHFILEDS))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have tried any number of different string combinations for the max() calc to no avail (not that this particular variation shows that).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Adding/Changing the following to the script doesn't give me the syntax error that I would recieve with the above, but it does give me a "The calculate value is invalid for the row with ObjectID = 0..." x 18,526 (or however many rows are in my table) and then does nothing to the table except populate my MAXSURGE field with 0's.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;SLOSHFILEDS = arcpy.ListFields(LAYERS,"","DOUBLE") fieldNameList = [] for field in SLOSHFILEDS: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not field.required: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldNameList.append(field.name) arcpy.CalculateField_management (LAYERS, "MAXSURGE", max(fieldNameList))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hard coding the field names into the formula works great, but of course, I will not always have the same number of fields or same field names to work with.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-John&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 12:04:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214982#M16571</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2012-08-29T12:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214983#M16572</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There may be a way to do this is a field calculator, but I would use this method. I think you may also want to modify your SLOSHFIELDS creation since you are extracting the name there and then trying to reference the required parameter and name parameter in your loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os
import string

LAYERS = arcpy.GetParameterAsText(0)
SLOSHFIELDS = [f for f in arcpy.ListFields(LAYERS,"","DOUBLE")]
fieldNameList = []

for field in SLOSHFIELDS :
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not field.required:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldNameList.append(field.name)

max_field = "MAXSURGE"
curs = arcpy.UpdateCursor(LAYERS)
for row in curs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; max_val = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldNameList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val = row.getValue(field)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if val &amp;gt; max_val:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; max_val = val
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(max_field, max_val)
&amp;nbsp;&amp;nbsp;&amp;nbsp; curs.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:32:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214983#M16572</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T10:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214984#M16573</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is a tricky problem, one thing i'm not quite sure of is if this is being done on a row by row basis.. so each row or feature in your table has the maxsurge value calculated.&amp;nbsp; If so I think this sort of thing probably wants to be done in the context of an update cursor rather than using the field calculator.&amp;nbsp; One way to handle this would be to get your list of fields, create an update cursor that has all the fields referenced, then construct a list that contains all the values you are interested in comparing,then use max() to get the largest values from the list you created.&amp;nbsp; Conceptually this might look something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;LAYERS = arcpy.GetParameterAsText(0)
SLOSHFILEDS = [f.name for f in arcpy.ListFields(LAYERS,"","DOUBLE") #presuming this filters your fields to the ones you want to compare
rows = arcpy.UpdateCursor(LAYERS) #leaving field names out of the cursor tool returns all fields
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; comp_values = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fname in SLOSHFILEDS:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comp_values.append(row.fname)
&amp;nbsp;&amp;nbsp;&amp;nbsp; max_val = max(comp_values)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.MAXSURGE = max_val
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:32:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214984#M16573</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-11T10:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214985#M16574</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;mzcoyle&lt;/STRONG&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;STRONG&gt;clthompson&lt;/STRONG&gt;&lt;SPAN&gt;, Thanks for your suggestions. Not to discourage you from making additional cursor suggestions (I'll given them a go if you provide them), but I've never had much luck with search cursors and usually try to avoid them--I just can't seem to wrap my head around them. I always end up with an infinite loop or receive some kind of error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried both of your suggestions and neither worked. With &lt;/SPAN&gt;&lt;STRONG&gt;mzcoyle&lt;/STRONG&gt;&lt;SPAN&gt;'s I receive "exceptions.AttributeError: Cursor: Error in parsing arguments." With &lt;/SPAN&gt;&lt;STRONG&gt;clthompson&lt;/STRONG&gt;&lt;SPAN&gt;'s I receive "exceptions.RuntimeError: Row: Field fname does not exist."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The bottom code I provided will run and populate the empty MAXSURGE field, however it populates the field with 0's after giving me "The calculate value is invalid for the row with ObjectID = 0..." error. If I try to concatenate the list object into a string value to add to my max() formula, e.g.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;SLOSHSTRING = str(fieldNameList)
calculation = "\"max(" + SLOSHSTRING + ")\""
arcpy.CalculateField_management (LAYERS, "MAXSURGE", calculation, "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I receive "The calculate value is invalid for the row" error. However if I hard code the max() calculation, e.g.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management (LAYERS, "MAXSURGE", "max( !SURGE90!, !SURGE90_1!, !SURGE90_12!, !SURGE90_13!, !SURGE90_14!, !MAXSURGE!)", "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;it works just fine and fairly quickly on 18,000+ rows.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I guess I'm to figure out how to get python to think that it's getting&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management (LAYERS, "MAXSURGE", "max( !SURGE90!, !SURGE90_1!, !SURGE90_12!, !SURGE90_13!, !SURGE90_14!, !MAXSURGE!)", "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;when it receives this&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.CalculateField_management (LAYERS, "MAXSURGE", max({ListObject}), "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Suggestions? With this or with cursors.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:32:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214985#M16574</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-11T10:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214986#M16575</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I had forgot to put in the row object in this line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;curs.updateRow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;That is most likely the issue. Cursors really are the best way to go about searching or updating data on a row by row basis.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Passing field objects to the calculate field tool as variables would require you to change them to strings from a list and add the ! to mark them as field variables to the field calculator. You'd have to do something funky like this I think.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;field_str = "!" + "!, !".join(fieldNameList) + "!"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 14:54:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214986#M16575</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-08-29T14:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214987#M16576</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;THANK YOU mzcoyle! Both worked beautifully.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 15:15:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214987#M16576</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2012-08-29T15:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214988#M16577</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;"exceptions.RuntimeError: Row: Field fname does not exist."&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Like i said "conceptually" &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; - without testing I wasn't sure how the value held by fname needs to be handled.&amp;nbsp; Ultimately that may need to be converted to a string... it may not even be possible in that context.&amp;nbsp; But its a good case to play with.&amp;nbsp; Cursors are really fairly simple - they allow you to step row by row through a table of data, performing some action on each row and then moving on to the next, and then when the get to the end, they stop.&amp;nbsp; Sounds like you found a solution though!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 15:38:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214988#M16577</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-08-29T15:38:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214989#M16578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you clthompson. I appreciate your help. I'll give cursors another look-see, may this time they'll click.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 15:43:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214989#M16578</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2012-08-29T15:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214990#M16579</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, this has got under my skin a little so I think here's a solution:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;LAYERS = arcpy.GetParameterAsText(0)
SLOSHFILEDS = [f.name for f in arcpy.ListFields(LAYERS,"","DOUBLE") #presuming this filters your fields to the ones you want to compare
rows = arcpy.UpdateCursor(LAYERS) #leaving field names out of the cursor tool returns all fields
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; comp_values = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; for fname in SLOSHFILEDS:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nom = fname.name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; comp_values.append(row.getValue(nom))
&amp;nbsp;&amp;nbsp;&amp;nbsp; max_val = max(comp_values)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.MAXSURGE = max_val
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically, the list of created as SLOSHFILEDS is a list of field objects, which have a variety of attributes including 'name' so using that as a value to pass into the getValue method you can populate your list of values for the row.&amp;nbsp; The rest of the code works as described earlier.&amp;nbsp; I like this approach because its simple and I'm lazy and don't like to write lots of lines of code. For loops and cursors FTW!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:32:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214990#M16579</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-11T10:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: Help with ListFields and CalculateField_management syntax</title>
      <link>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214991#M16580</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Ok, this has got under my skin a little&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Don't you hate when that happens? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll give this a go as well.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Aug 2012 17:47:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-with-listfields-and-calculatefield-management/m-p/214991#M16580</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2012-08-29T17:47:37Z</dc:date>
    </item>
  </channel>
</rss>

