<?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: Get Specific Field 'value' and return names and count. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218636#M16801</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't have time at the moment to mock up some code, but I will share a few thoughts:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Do you expect duplicate names?&amp;nbsp; I ask because wouldn't returning a list of names basically give the user the count as well?&amp;nbsp; Do you need to explicitly return both or would just a list of names work?&lt;/LI&gt;&lt;LI&gt;It looks like you are using the older/original cursors.&amp;nbsp; I recommend using the somewhat new-ish &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/what-is-the-data-access-module-.htm"&gt;Data Access module&lt;/A&gt; cursors.&lt;/LI&gt;&lt;LI&gt;I think the best approach here will be a combination of SQL and cursors.&amp;nbsp; Since the user will be picking a value from a well-structured, pre-defined list of facilities, using an SQL where clause to either create a selection set on a layer or define the search cursor will work and perform well.&amp;nbsp; Then, you only loop through the records returned and not all of the records.&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 31 Jan 2017 18:21:54 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2017-01-31T18:21:54Z</dc:date>
    <item>
      <title>Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218635#M16800</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;IMG __jive_id="312582" alt="" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/312582_high_school.png" style="width: 620px; height: 356px;" /&gt;&lt;/P&gt;&lt;P&gt;I have this attribute table where I am supposed to access the Field "Facility" and based on what type of facility the user enters, my code is supposed to return the names and count of that facility type. For example: I enter High School as my facility type. My code needs to return the names of the high schools, as well as how many high schools there are. Oh and this is supposed to operate as a function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = raw_input("Enter the workspace path: ")# c:\Scripts\Lab 6 Data&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; shapefile = "Schools.shp"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; work = arcpy.env.workspace&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sTyp = "HIGH SCHOOL"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def numSchools(work, shapefile, sTyp):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; sc = arcpy.SearchCursor(work, shapefile)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; field = "FACILITY"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in sc:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue(field)== sTyp&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return row.getValue("NAME")&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; numSchools(work, shapefile, sTyp) &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2017 18:02:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218635#M16800</guid>
      <dc:creator>AleahWorthem</dc:creator>
      <dc:date>2017-01-31T18:02:40Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218636#M16801</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't have time at the moment to mock up some code, but I will share a few thoughts:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Do you expect duplicate names?&amp;nbsp; I ask because wouldn't returning a list of names basically give the user the count as well?&amp;nbsp; Do you need to explicitly return both or would just a list of names work?&lt;/LI&gt;&lt;LI&gt;It looks like you are using the older/original cursors.&amp;nbsp; I recommend using the somewhat new-ish &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/what-is-the-data-access-module-.htm"&gt;Data Access module&lt;/A&gt; cursors.&lt;/LI&gt;&lt;LI&gt;I think the best approach here will be a combination of SQL and cursors.&amp;nbsp; Since the user will be picking a value from a well-structured, pre-defined list of facilities, using an SQL where clause to either create a selection set on a layer or define the search cursor will work and perform well.&amp;nbsp; Then, you only loop through the records returned and not all of the records.&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2017 18:21:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218636#M16801</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2017-01-31T18:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218637#M16802</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry, I do not need to return the names of the schools just the count. I works fine when I hard-coded it but I do not know how to change the sTyp to High School or Elementary School and return the correct output.&lt;/P&gt;&lt;P&gt;I am required to use Search Cursors to write my code. using arcpy or arcpy.da&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2017 19:04:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218637#M16802</guid>
      <dc:creator>AleahWorthem</dc:creator>
      <dc:date>2017-01-31T19:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218638#M16803</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How do you plan to get the user to input the data?&lt;/P&gt;&lt;P&gt;Will this be a model, a python addin or other?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My first thought would be&amp;nbsp;&lt;A class="link-titled" href="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/select-layer-by-attribute.htm" title="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/select-layer-by-attribute.htm"&gt;Select Layer By Attribute—Help | ArcGIS for Desktop&lt;/A&gt;&amp;nbsp;then &lt;A class="link-titled" href="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/get-count.htm" title="http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/get-count.htm"&gt;Get Count—Help | ArcGIS for Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;Just read your second post all the way.&lt;/P&gt;&lt;P&gt;Still would need to know what you intend for an input.&lt;/P&gt;&lt;P&gt;You could use a query on your search cursor to limit the number of records. Then loop through and count them.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2017 20:58:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218638#M16803</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2017-01-31T20:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218639#M16804</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The sTyp will be converted into a raw_input statement. It is just hard-coded here for convenience.&lt;/P&gt;&lt;P&gt;Instead of select layer by attribute, is there a way to get to the field "Facility" the value 'High School' by using SearchCursor()?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Jan 2017 21:07:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218639#M16804</guid>
      <dc:creator>AleahWorthem</dc:creator>
      <dc:date>2017-01-31T21:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218640#M16805</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Add a where clause to your search cursor&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" title="http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;SearchCursor—Help | ArcGIS for Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"Your\feature\class\here"&lt;/SPAN&gt;
fld &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"fieldName"&lt;/SPAN&gt;
sType &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"ValueYourLooking4"&lt;/SPAN&gt;
whereclause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""{} = '{}'"""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddFieldDelimiters&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;sType&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

count &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; whereclause&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; rows&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; rows&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count&lt;SPAN class="operator token"&gt;+=&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; count
&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:40:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218640#M16805</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2021-12-11T10:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Get Specific Field 'value' and return names and count.</title>
      <link>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218641#M16806</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For counting with cursors, Python's built in &lt;A href="https://docs.python.org/2/library/functions.html#sum" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;SPAN style="font-family: courier new,courier,monospace;"&gt;sum&lt;/SPAN&gt; &lt;/A&gt;works great, and is more idiomatic if you care about such things.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;"Your\feature\class\here"&lt;/SPAN&gt;
fld &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"fieldName"&lt;/SPAN&gt;
sType &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"ValueYourLooking4"&lt;/SPAN&gt;
whereclause &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"""{} = '{}'"""&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddFieldDelimiters&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;sType&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; whereclause&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; rows&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; count &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; sum&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; rows&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; count‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As much as I like working with Python's built-in functions, for simple counting of selections or subsets of records, especially with large data sets, the &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/get-count.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;Get Count &lt;/A&gt;tool is the quickest.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:40:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/get-specific-field-value-and-return-names-and/m-p/218641#M16806</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T10:40:37Z</dc:date>
    </item>
  </channel>
</rss>

